-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
102 lines (97 loc) · 2.31 KB
/
client.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
module.exports = {
name: "client",
ns: "websocket",
description: "Websocket Client",
async: true,
phrases: {
active: "Creating websocket client"
},
ports: {
input: {
url: {
type: "string",
title: "Url"
},
protocol: {
title: "Protocol",
type: "string",
"default": null
},
send: {
title: "Send",
type: "any",
async: true,
fn: function __SEND__(data, source, state, input, $, output, websocket) {
var r = function() {
if (!state.client) {
if ($.protocol) {
state.client = new websocket.w3cwebsocket($.url, $.protocol);
} else {
state.client = new websocket.w3cwebsocket($.url);
}
state.client.onmessage = function(event) {
output({
message: $.create(JSON.parse(event.data))
});
};
state.client.onerror = function(event) {
output({
error: $.create(event)
});
};
state.client.onclose = function(event) {
output({
close: $.create(event)
});
};
state.client.onopen = function(event) {
output({
client: $.create(state.client),
open: $.create(event)
});
};
}
if (state.client && state.client.readyState === state.client.OPEN) {
state.client.send(JSON.stringify($.send));
} else {
// should revoke input && re-queue
return false;
}
}.call(this);
return {
state: state,
return: r
};
}
}
},
output: {
client: {
type: "WebSocket",
title: "WebSocket"
},
open: {
type: "any",
title: "Open"
},
close: {
type: "any",
title: "Close"
},
message: {
type: "any",
title: "Message"
},
error: {
type: "Object",
title: "Error"
}
}
},
dependencies: {
npm: {
websocket: require('websocket')
}
},
state: {}
}