Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var WebSocketServer = require('ws').Server;
- var wss = new WebSocketServer({port: 8080});
- var jwt = require('jsonwebtoken');
- /**
- The way I like to work with 'ws' is to convert everything to an event if possible.
- **/
- function toEvent (message) {
- try {
- var event = JSON.parse(message);
- this.emit(event.type, event.payload);
- } catch(err) {
- console.log('not an event' , err);
- }
- }
- wss.on('connection', function(ws) {
- ws.on('message', toEvent)
- .on('authenticate', function (data) {
- jwt.verify(data.token, options, function (err, decoded) {
- //now is authenticated
- });
- });
- ws.send('something');
- });
Add Comment
Please, Sign In to add comment