forked from hypercomm/wonder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMessagingStub_NodeJS.js
135 lines (117 loc) · 4.21 KB
/
MessagingStub_NodeJS.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
/*********************************************************************************
* Messaging Stub Class
* For websocket
*/
function MessagingStub_NodeJS() {
this.ownRtcIdentity;
this.credentials;
this.websocket;
this.conversations=new Array();
}
/**
* SendMessage
* @param message... Message
*/
MessagingStub_NodeJS.prototype.sendMessage = function (message) {
console.log("C->S: ", message);
var full_message = new Object();
full_message.type = "message";
full_message.body = message;
// Multicast support for INVITE and UPDATE
if ((message.type == MessageType.INVITATION || message.type == MessageType.UPDATE) && message.body.peers) {
this.conversations.forEach(function (element, index, array) {
if (element.contextId == message.contextId) array.splice(index, 1);
});
var conversation = new Object();
conversation.contextId = message.contextId;
conversation.peers = message.body.peers;
this.conversations.push(conversation);
}
// Multicast support if to is empty
if (!message.to) {
var peers;
var that=this;
this.conversations.forEach(function (element, index, array) {
if (element.contextId == message.contextId) peers = element.peers;
});
message.from = message.from.rtcIdentity;
if(peers){
peers.forEach(function (element, index, array) {
full_message.to = element;
that.websocket.send(JSON.stringify(full_message));
});
}
return
}
// From and To Identities are changed into strings containing rtcIdentities
// If To is an array, it sends it to the first position
message.from = message.from.rtcIdentity;
if (message.to instanceof Array) {
message.to.every(function (element, index, array) {
array[index] = element.rtcIdentity;
});
full_message.to = message.to[0];
} else {
message.to = new Array(message.to.rtcIdentity);
full_message.to = message.to[0];
}
this.websocket.send(JSON.stringify(full_message));
};
/**
* Connect
*/
MessagingStub_NodeJS.prototype.connect = function(ownRtcIdentity, credentials, callbackFunction) {
this.ownRtcIdentity = ownRtcIdentity;
this.credentials = credentials;
//DEFINE THE HOST ADDR
var signaling_server = "127.0.0.1:1337";
//signaling_server="172.16.62.66:1337";
//signaling_server = "150.140.184.247:1337";
// If connect was already executed succesfully, it won't try to connect again, just execute the callback.
if (this.websocket) {
console.log("Websocket connection already opened, executing callback function: ");
callbackFunction();
return;
}
console.log('Opening channel: ws://' + signaling_server);
this.websocket = new WebSocket('ws://' + signaling_server);
var socket = this.websocket;
this.websocket.onopen = function() {
var message = new Object();
message.type = "login";
message.from = ownRtcIdentity;
socket.send(JSON.stringify(message));
console.log("Websocket connection opened and logging in as: " + ownRtcIdentity);
callbackFunction();
};
this.websocket.onerror = function() {
console.log("Websocket connection error");
};
this.websocket.onclose = function() {
console.log("Websocket connection closed");
};
var that = this;
// Maps the websocket listener to the MessagingStub listeners, and notifies filtering
// by contextId
this.websocket.onmessage = function(full_message) {
// IF it doesnt have contextID, it is the application.
var message = JSON.parse(full_message.data).body;
console.log("Received: ", message); //#############firefox-test#####################
Idp.getInstance().createIdentity(message.from, function(identity) {
message.from = identity;
// createIdentities can take array or single rtcIdentity and always returns an Array in the callback result
Idp.getInstance().createIdentities(message.to, function(identityArr) {
message.to = identityArr;
that.baseStub.sendOtherMessages(message);
});
});
};
}
/**
* Disconnects from the server.
*/
MessagingStub_NodeJS.prototype.disconnect = function() {
this.websocket.close();
this.websocket = null;
console.log("Websocket connection disconnected");
};