From a5a9bd76415b72ca3bcbe7a25b923d62ef2068f6 Mon Sep 17 00:00:00 2001 From: Mika Rautio Date: Fri, 8 Feb 2019 08:36:39 +0200 Subject: [PATCH] Add WebSocket support --- config.cson | 2 ++ package.json | 84 +++++++++++++++++++++++++++------------------------ server.coffee | 15 ++++++++- 3 files changed, 60 insertions(+), 41 deletions(-) diff --git a/config.cson b/config.cson index e96a2f4..b7d47da 100644 --- a/config.cson +++ b/config.cson @@ -2,6 +2,8 @@ servers: httpPort: 8080 udpPort: 9909 + webSocketPort: 9910 + webSocketHost: "0.0.0.0" hosts: enttec1: # path: "/dev/serial/by-id/usb-FTDI_FT245R_USB_FIFO_ENP9D7H7-if00-port0" diff --git a/package.json b/package.json index 9393f62..d4fab5b 100644 --- a/package.json +++ b/package.json @@ -1,41 +1,45 @@ { - "name": "effectserver", - "version": "0.1.1", - "author": "Esa-Matti Suuronen", - "contributors": [ - "Tommi Teistelä" - ], - "homepage": "https://github.com/Instanssi/effectserver", - "description": "UDP packet abstraction for multiple DMX controllers", - "bugs": { - "url": "https://github.com/Instanssi/effectserver/issues/" - }, - "repository": { - "type": "git", - "url": "https://github.com/Instanssi/effectserver.git" - }, - "scripts": { - "test": "mocha --compilers coffee:coffee-script/register", - "start": "coffee server.coffee" - }, - "main": "./lib/index", - "dependencies": { - "coffee-script": "~1.12.3", - "underscore": "~1.8.3", - "serialport": "~4.0.0", - "underscore.string": "~3.3.4", - "async": "~1.5.2", - "jade": "~1.11.0", - "stylus": "~0.53.0", - "socket.io": "~0.9.16", - "cson": "~3.0.2", - "piler": "~0.5.1", - "express": "~2.5.11", - "hbs": "~4.0.0" - }, - "devDependencies": { - "mocha": "~3.2.0", - "should": "~11.2.0", - "supervisor": "~0.12.0" - } -} \ No newline at end of file + "name": "effectserver", + "version": "0.1.1", + "author": "Esa-Matti Suuronen", + "contributors": [ + "Tommi Teistelä" + ], + "homepage": "https://github.com/Instanssi/effectserver", + "description": "packet abstraction for multiple DMX controllers", + "bugs": { + "url": "https://github.com/Instanssi/effectserver/issues/" + }, + "repository": { + "type": "git", + "url": "https://github.com/Instanssi/effectserver.git" + }, + "scripts": { + "test": "mocha --compilers coffee:coffee-script/register", + "start": "coffee server.coffee" + }, + "main": "./lib/index", + "dependencies": { + "coffee-script": "~1.12.3", + "underscore": "~1.8.3", + "serialport": "~4.0.0", + "underscore.string": "~3.3.4", + "async": "~1.5.2", + "jade": "~1.11.0", + "stylus": "~0.53.0", + "socket.io": "~0.9.16", + "cson": "~3.0.2", + "piler": "~0.5.1", + "express": "~2.5.11", + "hbs": "~4.0.0", + "ws": "~6.1.3" + }, + "devDependencies": { + "mocha": "~3.2.0", + "should": "~11.2.0", + "supervisor": "~0.12.0" + }, + "optionalDependencies": { + "bufferutil": "^4.0.1" + } +} diff --git a/server.coffee b/server.coffee index acdb541..4e15e75 100644 --- a/server.coffee +++ b/server.coffee @@ -1,5 +1,6 @@ CSON = require "cson" dgram = require "dgram" +ws = require "ws" fs = require "fs" util = require "util" EventEmitter = require "events" @@ -35,7 +36,7 @@ onConfigRead = (error, config) -> console.log "FIREWALL", firewall , 1000 - udpserver.on "message", (packet, rinfo) -> + processPacket = (packet, rinfo) -> try cmds = packetParse packet catch e @@ -77,6 +78,18 @@ onConfigRead = (error, config) -> if not firewall? websocket.sockets.volatile.emit "cmds", results + wsserver = new ws.Server { port: config.servers.webSocketPort, host: config.servers.webSocketHost } + + wsserver.on "listening", -> + console.log "Now listening WebSocket on #{ config.servers.webSocketHost }:#{ config.servers.webSocketPort }" + + wsserver.on 'connection', (socket, req) -> + socket.on 'message', (packet) -> + processPacket packet, { address: req.connection.remoteAddress } + + udpserver.on "message", (packet, rinfo) -> + processPacket packet, rinfo + udpserver.on "listening", -> console.log "Now listening on UDP port #{ config.servers.udpPort }" udpserver.bind config.servers.udpPort