Network socket emulation for mudb.
In mulocal-socket, no real network connections are established so no Web servers are needed, meaning any mudb applications using mulocal-socket can run entirely in a browser. This can be favorable to development in that
var mudb = require('mudb')
var muLocalSocket = require('mulocal-socket')
var socketServer = muLocalSocket.createLocalSocketServer()
var muServer = new mudb.MuServer(socketServer)
muServer.start({ /* event handlers */ })
var socket = muLocalSocket.createLocalSocket()
var muClient = new mudb.MuClient(socket)
mClient.start({ /* event handlers */ })
npm i mulocal-socket
Purely instructive types used to describe the API:
SessionId: stringData: Uint8Array | stringSocketState: an enum consisting of three members
SocketState.INITSocketState.OPENSocketState.CLOSEDSocketServerState: an enum consisting of three members
SocketServerState.INITSocketServerState.RUNNINGSocketServerState.SHUTDOWNcreateLocalSocketServer()A factory returning a new instance of MuLocalSocketServer.
createLocalSocket(spec)Spawns and associates two new instances of MuLocalSocket, then returns the client-side socket to be used to create a MuClient.
spec:object
sessionId:SessionId: a unique session id used to identify a clientserver:MuLocalSocketServer: the socket serverMostly you should only use the factory methods instead of the constructors.
MuLocalSocketServerA MuLocalSocketServer is a pseudo socket server that can be used to create a MuServer. It does not create any server-side sockets and is only responsible for accumulating and closing sockets.
state:SocketServerStateA tri-valued field determining the availability of the socket server. It is initialized to SocketServerState.INIT.
clients:MuLocalSocket[]Server-side sockets, one per client through which the server communicates with the client.
start(spec)Hooks handlers and accumulates sockets. state is set to SocketServerState.RUNNING.
spec:object
ready() called when the socket server is readyconnection(socket:MuLocalSocket) called when a new server-side socket is addedclose(error?) called when the socket server is shut downclose()Closes all sockets. state is set to SocketServerState.SHUTDOWN.
MuLocalSocket(sessionId, server)A MuLocalSocket can either be a client-side or server-side socket and every two MuLocalSockets should be associated to form an exclusive pair. A MuLocalSocket can be used to create a MuClient when it is used as a client-side socket.
sessionId:SessionId: a unique session id used to identify a clientserver:MuLocalSocketServer: the socket serversessionId:SessionIdThe unique session id identifying the client.
state:SocketStateA tri-valued field determining the availability of the socket. It is initialized to SocketState.INIT.
open(spec)Hooks handlers and drains pending messages. state is set to SocketState.OPEN.
spec:object
ready() called when the socket is ready to receive datamessage(data:Data, unreliable:boolean) called when receiving dataclose(error?) called when the socket is closedsend(data, unreliable?)Sends data to the associated socket, which will be delivered either in order or out of order, depending on the value of unreliable.
data:Data data to be sent, can either be a JSON string or a Uint8Arrayunreliable?:boolean optional, the data will be delivered out of order if set to trueclose()Closes the socket and the socket on the other end. state is set to SocketState.CLOSED.
Copyright (c) 2017 Mikola Lysenko, Shenzhen Dianmao Technology Company Limited