1.0.0 • Published 8 years ago
fictorial-base-wss v1.0.0
A generic JSON-over-WebSockets server.
Basic Usage
Using defaults:
const {server, clients} = require('fictorial-base-wss')((socket, msg) => {
...
})
Or being explicit:
const {server, clients} = require('fictorial-base-wss')({
port: 8000,
maxMissedPongs: 3,
pingEveryMillis: 29000,
logger: new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({ filename: 'somefile.log' })
]
}),
onMessage: function (socket, msg) {
// ...
},
onDisconnect: function (socket) {
// ...
}
})
Sending Data
The socket
is a WebSocket from the ws
module. Thus, go ahead and
send it something in your handlers. socket.send(JSON.stringify({...}))
Error Handling
If the handler throws an error that has a .fatal
or .close
property
set to true
, the client WebSocket connection will be disconnected.
Client Identity
If the handler wishes to identify clients by some name, it should set
.clientId
to that name. Then, connected clients may be found via
clients[someId]
.
1.0.0
8 years ago