1.0.0 • Published 8 years ago
@kyuuseiryuu/websocket-handler v1.0.0
websocket-handler
Quick Start
- clone this repository
- run
npm i && npm start - now server is running...
API
setEventListener(string: eventName, function: callback)broadcast(function: callback(connection))- array[] connectionsKey
getAllConnectionsKey() get(string: connectionKey, function: callback(Connection))listen(number: port, string: host, function: callback)setActionMap(actionMap)const actionMap = { actionName(Message, connection) { // Use Message and connection to do something const jumpEvent = true; return jumpEvent; }, }setAction(string: actionName, callback(Message, Connection: current connection))send(object|string: message, Connection: target connection)Not support binary data, if you want to send binary data you can use Connection object.
Event Support
beforeJoinnew connection join manage beforeafterJoinnew connection join manage afterbeforeQuitconnection quit manage beforeafterQuitconnection quit manage aftercreatewhen connection createjsonwhen connection receive a json stringwhen
jsonevent listener is undefined, will try to calltextevent listener.textwhen connection receive a texterrorWebSocket eventcloseWebSocket event
Event Params
| Event | params |
|---|---|
| beforeJoin/afterJoin | (connection, allConnectionsKey) |
| beforeQuit/afterQuit | (connection, allConnectionsKey) |
| create | (connection) |
| json | (Message, connection) |
| text | (Message, connection) |
| error | (WebSocketErrorEvent) |
| close | (connection) |
Data Flow
- The request will transform to JSON object,
- If request transform failed will call
textevent and over. - If request has
SYS_ACTIONproperty, will try to map action - If action mapper return true to jump event then over this request
- Else will call
jsonevent.
License
MIT
1.0.0
8 years ago