Created
December 6, 2018 21:13
-
-
Save dheerajsuthar/0f2aad2677f37e3e053ac255c503e4a8 to your computer and use it in GitHub Desktop.
WebSocket Server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const WebSocketServer = require('ws').Server; | |
const express = require('express'); | |
const path = require('path'); | |
const server = require('http').createServer(); | |
const PubSubManager = require('./pubsub'); | |
const app = express(); | |
const pubSubManager = new PubSubManager(); | |
app.use(express.static(path.join(__dirname, '/public'))); | |
const wss = new WebSocketServer({ server: server }); | |
wss.on('connection', (ws, req) => { | |
console.log(`Connection request from: ${req.connection.remoteAddress}`); | |
ws.on('message', (data) => { | |
console.log('data: ' + data); | |
const json = JSON.parse(data); | |
const request = json.request; | |
const message = json.message; | |
const channel = json.channel; | |
switch (request) { | |
case 'PUBLISH': | |
pubSubManager.publish(ws, channel, message); | |
break; | |
case 'SUBSCRIBE': | |
pubSubManager.subscribe(ws, channel); | |
break; | |
} | |
}); | |
ws.on('close', () => { | |
console.log('Stopping client connection.'); | |
}); | |
}); | |
server.on('request', app); | |
server.listen(8080, () => { | |
console.log('Server listening on http://localhost:8080'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment