-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnodechat.js
146 lines (123 loc) · 4.64 KB
/
nodechat.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
Copyright (c) Lightstreamer Srl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var
// Imports
MetadataProvider = require('lightstreamer-adapter').MetadataProvider,
DataProvider = require('lightstreamer-adapter').DataProvider,
net = require('./robustconnect'),
inspect = require('util').inspect,
commandLineArgs = require('command-line-args'),
// Data requests/replies socket channel
dataStream,
// Metadata requests/replies socket channel
metadataStream,
// The data provider object
dataProvider,
// The data provider object
metadataProvider,
// JS timer for the simulated data push
subscribed = false,
// User data just for the demo
sessions = [];
const optionDefinitions = [
{ name: 'host', type: String },
{ name: 'tls', type: Boolean, defaultOption: false },
{ name: 'metadata_rrport', type: Number },
{ name: 'data_rrport', type: Number },
{ name: 'user', type: String },
{ name: 'password', type: String }
];
const options = commandLineArgs(optionDefinitions);
var credentials;
if (options.user != null) {
credentials = { user: options.user, password: options.password };
} else {
credentials = null;
}
// Create socket connections
net.createConnection(options.data_rrport, options.host, options.tls, function(stream) {
dataStream = stream;
initDataProvider();
});
net.createConnection(options.metadata_rrport, options.host, options.tls, function(stream) {
metadataStream = stream;
initMetadataProvider();
});
function initDataProvider() {
//Create the data provider object from the lightstreamer module
dataProvider = new DataProvider(dataStream, null, null, credentials);
//Handle subscribe event
dataProvider.on('subscribe', function(itemName, response) {
console.log("Subcribed item: " + itemName);
if (itemName === "chat_room") {
subscribed = true;
response.success();
} else {
response.error("No such item", "subscription");
}
});
// Handle unsubscribe event
dataProvider.on('unsubscribe', function(itemName, response) {
console.log("Unsubscribed item: " + itemName);
if (itemName === "chat_room") {
subscribed = false;
response.success();
} else {
response.error("No such item", "subscription");
}
});
}
function initMetadataProvider() {
// Create the metadata provider object from the lightstreamer module
metadataProvider = new MetadataProvider(metadataStream, {
distinctSnapLen: 30,
itemAllowedModes: {distinct: true},
userAllowedModes: {distinct: true},
}, credentials);
// The default management of getItems and getSchema, which treats
// group and schema names as comma-separated lists, is suitable
// for this demo
// Handle new session event and store user session data
metadataProvider.on('notifyNewSession', function(request, response) {
console.log("New session: " + inspect(request));
sessions[request.sessionId] = request.contextProperties;
response.success();
});
// Handle close session event and remove stored session data
metadataProvider.on('notifySessionClose', function(request, response) {
console.log("Close session: " + inspect(request));
delete(sessions[request.sessionId]);
response.success();
});
// Handle any incoming message and push it to the chat_room item
metadataProvider.on('notifyUserMessage', function(request, response) {
console.log("New user message: " + inspect(request));
if (!subscribed) {
response.error("Unexpected message", "notification");
return;
}
if (!sessions[request.sessionId]) {
response.error("Session lost! Please reload the browser page(s).", "notification");
return;
}
var session = sessions[request.sessionId];
var userMessage = request.userMessage.split("|")[1];
dataProvider.update("chat_room", false, {
'raw_timestamp': new Date().getTime() + '',
'IP': session.REMOTE_IP,
'nick': session.USER_AGENT,
'message': userMessage
});
response.success();
});
}