-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.js
76 lines (56 loc) · 1.42 KB
/
websocket.js
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"use strict";
var WebSocketServer = require('ws').Server;
var wsS;
var Me = this;
exports.connect = function (host,port)
{
wsS = new WebSocketServer(
{
host: host,
port: port
}); // start websocket server
console.log ("WebSocket Listener at " + host + ":" + port);
wsS.on('connection', onConnect_Handler);
}
exports.onMessageReceived = undefined;
exports.sendMessage = function (message)
{
if (Me.ws != null)
{
try
{
Me.ws.send (message , {binary: false});
}
catch (e)
{
Me.ws = undefined;
}
}
}
function onConnect_Handler(ws)
{
Me.ws = ws;
console.log("WebSocket Listener Active");
function onWsMessage(message, flags)
{
//console.log(message);
if (Me.onMessageReceived!= undefined)
{
Me.onMessageReceived (message);
}
}
function onWsClose(code)
{
// code: https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent
console.log("closing %s", code);
ws = undefined;
}
function onWsError(ws, err)
{
console.error('onWsError: Client #%d error: %s', ws.id, JSON.stringify(err));
//_dumpError.dumperror(err);
}
ws.on('message', onWsMessage);
ws.on('close', onWsClose);
ws.on('error', onWsError);
}