UNPKG

530 BJavaScriptView Raw
1"use strict";
2
3const koa = require('koa'),
4 route = require('koa-route'),
5 websockify = require('../');
6
7const app = websockify(koa());
8
9// Note it's app.ws.use and not app.use
10app.ws.use(route.all('/test/:id', function* (next) {
11 // `this` is the socket passed from WebSocketServer to connection listeners
12 this.send('Hello World');
13 this.on('message', function(message) {
14 // do something with the message from client
15 });
16 // yielding `next` will pass the socket on to the next ws middleware
17}));
18
19
20app.listen(3000);