forked from jmesnil/stomp-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstomp-node.js
108 lines (94 loc) · 2.51 KB
/
stomp-node.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Generated by CoffeeScript 1.7.1
/*
Stomp Over WebSocket http://www.jmesnil.net/stomp-websocket/doc/ | Apache License V2.0
Copyright (C) 2013 [Jeff Mesnil](http://jmesnil.net/)
*/
(function() {
var Stomp, net, overTCP, overWS, wrapTCP, wrapWS;
Stomp = require('./stomp');
net = require('net');
Stomp.Stomp.setInterval = function(interval, f) {
return setInterval(f, interval);
};
Stomp.Stomp.clearInterval = function(id) {
return clearInterval(id);
};
wrapTCP = function(port, host) {
var socket, ws;
socket = null;
ws = {
url: 'tcp:// ' + host + ':' + port,
send: function(d) {
return socket.write(d);
},
close: function() {
return socket.end();
}
};
socket = net.connect(port, host, function(e) {
return ws.onopen();
});
socket.on('error', function(e) {
return typeof ws.onclose === "function" ? ws.onclose(e) : void 0;
});
socket.on('close', function(e) {
return typeof ws.onclose === "function" ? ws.onclose(e) : void 0;
});
socket.on('data', function(data) {
var event;
event = {
'data': data.toString()
};
return ws.onmessage(event);
});
return ws;
};
wrapWS = function(url) {
var WebSocketClient, connection, socket, ws;
WebSocketClient = require('websocket').client;
connection = null;
ws = {
url: url,
send: function(d) {
return connection.sendUTF(d);
},
close: function() {
return connection.close();
}
};
socket = new WebSocketClient();
socket.on('connect', function(conn) {
connection = conn;
ws.onopen();
connection.on('error', function(error) {
return typeof ws.onclose === "function" ? ws.onclose(error) : void 0;
});
connection.on('close', function() {
return typeof ws.onclose === "function" ? ws.onclose() : void 0;
});
return connection.on('message', function(message) {
var event;
if (message.type === 'utf8') {
event = {
'data': message.utf8Data
};
return ws.onmessage(event);
}
});
});
socket.connect(url);
return ws;
};
overTCP = function(host, port) {
var socket;
socket = wrapTCP(port, host);
return Stomp.Stomp.over(socket);
};
overWS = function(url) {
var socket;
socket = wrapWS(url);
return Stomp.Stomp.over(socket);
};
exports.overTCP = overTCP;
exports.overWS = overWS;
}).call(this);