https://github.com/marihachi/websocket-events
user events for WebSocket-Node
https://github.com/marihachi/websocket-events
events json nodejs websocket
Last synced: 2 months ago
JSON representation
user events for WebSocket-Node
- Host: GitHub
- URL: https://github.com/marihachi/websocket-events
- Owner: marihachi
- License: mit
- Created: 2017-06-05T20:22:12.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-12-04T17:49:56.000Z (over 7 years ago)
- Last Synced: 2024-04-24T15:26:26.490Z (about 2 years ago)
- Topics: events, json, nodejs, websocket
- Language: JavaScript
- Size: 7.81 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# websocket-events
user events for [WebSocket-Node](https://github.com/theturtle32/WebSocket-Node) connections.
[](https://nodei.co/npm/websocket-events/)
## Installation
npm i websocket-events
## Usage
```js
const websocket = require('websocket');
const events = require('websocket-events');
/* -- instance creation of http.Server is omitted -- */
const server = new websocket.server({httpServer: http});
server.on('request', request => {
const connection = request.accept();
connection.on('error', err => {
if (err.userEventError) {
console.log('user event error:', err);
}
else {
console.log('error:', err);
}
});
connection.on('close', (reasonCode, description) => {
console.log('closed:', reasonCode, description);
});
// use here
const options = {};
events(connection, options);
connection.on('user-event', data => {
console.log('user-event:', data);
});
// this listener is only used when not set listeners
connection.on('default', event => {
console.log('default listener:', event.name, event.content);
});
// send user event as message of json data
connection.send('user-event', { hoge: 'piyo' });
// send normal text message
connection.send('normal message');
});
```
:bulb: The example is used on the server, but it can also be used on the client.
## Usable Options
### eventKeyName
The name used as key of event name.
Default value is `@event`.
### defaultEventName
The name used when event listeners is not set.
Default value is `default`.
## License
MIT