muworker-socket
v0.1.7
Published
Run mudb server in Web Worker
Downloads
5
Maintainers
Readme
muworker-socket
Web Worker made available to mudb
. Suitable for creating games with a single-player mode by running the server in a separate thread to allow better user experience.
example
Both browserify
and webworkify
are required to run the example.
worker.js
const { createWorkerSocketServer } = require('muworker-socket/server')
const { MuServer } = require('mudb/server')
// source of worker should go into `module.exports`
module.exports = () => {
const socketServer = createWorkerSocketServer()
const muServer = new MuServer(socketServer)
socketServer.listen()
muServer.start(/* listeners */)
}
client.js
// `webworkify` enables workers to `require()`
const work = require('webworkify')
const { createWorkerSocket } = require('muworker-socket/socket')
const { MuClient } = require('mudb/client')
const serverWorker = work(require('./worker.js'))
const socket = createWorkerSocket({
sessionId: Math.random().toString(36).substr(2),
serverWorker: serverWorker,
})
const muClient = new MuClient(socket)
muClient.start(/* listeners */)
table of contents
1 install
npm i muworker-socket
2 api
2.1 interfaces
Purely instructive types used to describe the API:
SessionId
:string
2.2 createWorkerSocketServer()
A factory returning a new instance of MuWorkerSocketServer
.
2.3 createWorkerSocket(spec)
A factory returning a new instance of MuWorkerSocket
.
spec:object
sessionId:SessionId
: a unique session id used to identify a clientserverWorker:Worker
: the worker in which the server is running
2.4 MuWorkerSocketServer
A MuWorkerSocketServer
is a pseudo socket server that can be used to instantiate a MuServer
.
2.4.1 listen()
Starts the socket server listening for connections.
2.5 MuWorkerSocket
A MuLocalSocket
is a pseudo client-side socket that can be used to instantiate a MuClient
.
credits
Copyright (c) 2018 He Diyi, Shenzhen Dianmao Technology Company Limited