Lightweight WebSocketServer wrapper lib using ws-wrapper to wrap connected WebSockets
npm install ws-server-wrapperLightweight WebSocketServer wrapper lib using ws-wrapper
and ws to wrap connected WebSockets. The
only dependency is ws-wrapper itself.
```
npm install ws-server-wrapper
See ws-wrapper README or the API
documentation below for more details.
Quick Server-side Example:
Use ws-server-wrapper to wrap
the WebSocket.Server:
`javascriptthis
const WebSocketServer = require("ws").Server
, WebSocketServerWrapper = require("ws-server-wrapper");
var wss = new WebSocketServer({port: 3000});
var serverWrapper = new WebSocketServerWrapper(wss);
// Send "msg" event to all connected clients
serverWrapper.emit("msg", "Hello!");
// For all connected clients, listen for the "ping" request on the channel "pointless"
serverWrapper.of("pointless").on("ping", function() {
// refers to the "pointless" channel for the socket who sent the "ping" request`
// Let's just respond to the request with the value "pong"
return "pong";
});
Class: WebSocketServerWrapper
A WebSocketServerWrapper simply wraps around a
WebSocket.Server
to give you well-deserved functionality. :)
server = new WebSocketServerWrapper(webSocketServerInstance[, options]);
Constructs a new WebSocketServerWrapper, and binds it to the native
WebSocketServer instance from ws.
- webSocketServerInstance - the native WebSocketServer instanceoptions
- - options passed to each WebSocketWrapper constructor when arequestTimeout
WebSocket connects. See the ws-wrapper API
for details.
- - See the ws-wrapper APIheartbeatInterval
API for more info. Defaults to 2 minutes if not specified.
- - If set, a "ping" will be sent to all connectedheartbeatInterval
sockets every milliseconds. If a "pong" response
is not received by the start of the next ping, the connection will be
terminated. Defaults to 1 minute if not specified. Set to a falsy value
to disable heartbeats.
Events
- Event: "connection" - Emitted when a WebSocket connects to the WebSocketServer
- socket - A WebSocketWrapper instance, wrapping a native WebSocketrequest
- - A http.IncomingMessagesocket
instance.
- Event: "disconnect" - Emitted when a WebSocket disconnects from the WebSocketServer
- - A WebSocketWrapper instance, wrapping a native WebSocketerr
- Event: "error" - Emitted when an error occurs on the WebSocketServer
-
The EventEmitter-like API looks like this:
See the ws-wrapper API documentation
for more details.
- server.on(eventName, listener)listener
Adds the function to the end of the listeners array for theeventName
event named for all connected sockets, now and in the future.eventName
When an event or request matching the is received by anylistener
connected WebSocket, the is called.
Values returned by the listener callback are used to respond tolistener
requests. If the return value of the is a Promise, thelistener
response to the request will be sent once the Promise is resolved or
rejected; otherwise, the return value of the is sent back to
the remote end immediately.
If the inbound message is a simple event (not a request), the return
value of the listener is ignored.server.removeListener(eventName, listener)
- listener
Removes the specified from the listener array for the eventeventName
named .server.removeAllListeners([eventName])
- eventName
Removes all listeners, or those of the specified .server.eventNames()
- server.listeners(eventName)
Returns an array listing the events for which the emitter has registered
listeners.
- eventName
Returns a copy of the array of listeners for the event named .server.emit(eventName[, ...args])
- eventName
Sends an event to all connected WebSockets with the specified eventName
calling all listeners for on the remote end, in the order they were
registered, passing the supplied arguments to each.
Note: server.once() and server.request() are not supported at this time.
Channel API:
- server.of(channelName)channelName
Returns the channel with the specified . Every channel has the
same EventEmitter-like API as described above for sending and handling
channel-specific events for all connected sockets.
Other methods and properties:
- server.sockets - A Setserver.close()
of connected WebSocketWrappers.
-
Closes the native WebSocketServer
The WebSocketServerWrapper will automatically (by default) ping all open sockets
on a regular basis. If there is no "pong" response by the start of the next ping,
the connection will be assumed to be "broken" and will be terminated automatically.
See options.heartbeatInterval` for more information. Also see the approached
outlined here.