-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.coffee
61 lines (51 loc) · 1.65 KB
/
server.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
`#!/usr/bin/env node
`
utils = require "./utils.js"
utils.extend global, utils
optimist = require "optimist"
args = optimist.usage("Usage: $0 [--port=PORT] [--host=ADDRESS] [--sock=PATH] [--mode=MODE]")
.alias("h", "help")
.default("port", config.port)
.default("host", config.host)
.default("sock", config.sock)
.default("mode", config.mode)
.argv
if args.help
optimist.showHelp()
process.exit 0
responseHandlers = {}
webSock = null
WSS = require("ws").Server
wss = new WSS port: args.port, host: args.host
wss.on "connection", (ws) ->
console.log "#{new Date().toString()}: websocket client connected"
webSock = ws
ws.on "message", (msg) ->
responseHandlers[JSON.parse(msg).clientId]? msg
uniqueId = Math.floor(2000000000 * Math.random()).toString()
clientId = 0
handleWebsocketError = (request) ->
responseHandlers[request.clientId]? JSON.stringify extend request, error: "websocket is not connected"
responseHandlers = {}
webSock = null
server = require("net").createServer (sock) ->
clientId += 1
myClientId = "#{uniqueId}-#{clientId}"
responseHandlers[myClientId] = sock.write.bind sock
sock.on "data", (data) ->
try
request = JSON.parse data
catch
console.error "failed to parse JSON: #{data}"
try
extend request, clientId: myClientId
webSock.send JSON.stringify(request), (err) ->
handleWebsocketError request if err
catch
handleWebsocketError request
sock.on "close", ->
delete responseHandlers[myClientId]
require("fs").unlink args.sock, ->
server.listen args.sock, ->
require("fs").chmod args.sock, args.mode, ->
console.log "listening on: #{args.sock}"