2018-08-30 10:49:09 +00:00
|
|
|
require('dotenv').config();
|
|
|
|
const WebSocket = require('ws');
|
|
|
|
const WebSocketStream = require('websocket-stream');
|
2018-08-29 12:24:38 +00:00
|
|
|
|
2018-08-29 17:20:48 +00:00
|
|
|
|
|
|
|
|
2018-08-30 10:49:09 +00:00
|
|
|
class WebSocketServer {
|
|
|
|
constructor() {
|
|
|
|
this.io = new WebSocket.Server({
|
|
|
|
port: process.env.PORT || 1337,
|
2018-08-29 17:20:48 +00:00
|
|
|
});
|
|
|
|
|
2018-08-30 10:49:09 +00:00
|
|
|
this.stream = WebSocketStream.createServer({
|
|
|
|
server: this.io,
|
|
|
|
}, this.handleStream)
|
2018-08-29 17:20:48 +00:00
|
|
|
|
2018-08-30 10:49:09 +00:00
|
|
|
this.createEventListeners();
|
2018-08-29 17:20:48 +00:00
|
|
|
}
|
|
|
|
|
2018-08-29 12:24:38 +00:00
|
|
|
|
2018-08-30 10:49:09 +00:00
|
|
|
handleStream(stream, req) {
|
|
|
|
console.log(stream);
|
2018-08-29 17:20:48 +00:00
|
|
|
}
|
2018-08-30 10:49:09 +00:00
|
|
|
createEventListeners() {
|
|
|
|
this.io.on('connection', (io) => {
|
|
|
|
io.on('message', (msg) => {
|
|
|
|
console.log('Recieved: %s', msg);
|
|
|
|
})
|
|
|
|
})
|
2018-08-29 17:20:48 +00:00
|
|
|
}
|
2018-08-29 12:24:38 +00:00
|
|
|
}
|
2018-08-29 17:20:48 +00:00
|
|
|
|
|
|
|
|
2018-08-30 10:49:09 +00:00
|
|
|
const wss = new WebSocketServer();
|