forked from xwiki-labs/chatflux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatflux.js
131 lines (113 loc) · 3.92 KB
/
chatflux.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
/* globals window, WebSocket */
require(['netflux.js',
'./bower_components/jquery/dist/jquery.js'], function (Netflux) {
var $ = window.jQuery;
var $backscroll
var connect = function (url) {
Netflux._onPeerMessage = onPeerMessage;
var channel = window.location.hash.substring(1) || null;
var options = {
signaling: url,
topology: 'StarTopologyService',
protocol: 'WebSocketProtocolService',
connector: 'WebSocketService'
};
return new Promise(function(resolve, reject) {
// Connect to the WebSocket server
// Join a WebChannel
Netflux.join(channel, options).then(function(wc) {
wc.onMessage = onMessage;
wc.onJoining = onJoining;
wc.onLeaving = onLeaving;
// Request the history of this channel from the peer with the best link quality
var hc;
wc.peers.forEach(function (p) { if (!hc || p.linkQuality > hc.linkQuality) { hc = p; } });
hc.send(JSON.stringify(['GET_HISTORY', wc.id]));
resolve(wc);
}, function(error) {
reject(error);
});
});
};
var logMsg = function (s) {
$backscroll.val(function (i, v) { return v + '\n' + s; });
$backscroll.scrollTop($backscroll[0].scrollHeight);
};
var onLeaving = function(peer) {
logMsg('* ' + peer + ' has left');
}
var onJoining = function(peer) {
logMsg('* ' + peer + ' has joined');
}
var onMessage = function (peer, msg) {
logMsg('<' + peer + '> ' + msg);
};
var onPeerMessage = function (peer, msg) {
if(peer === '_HISTORY_KEEPER_') {
var msgHistory = JSON.parse(msg[4]);
logMsg('*' + msgHistory[1] + '* ' + msgHistory[4]);
}
else {
logMsg('' + peer + '>> ' + msg[4]);
}
};
// Find a peer in a channel
var findPeerById = function(peerId, wc) {
var index = -1;
var peers = wc.peers;
for(var i=0; i<peers.length; i++) {
if(peers[i].id === peerId) {
index = i;
break;
}
}
return peers[i] || null;
}
var send = function (webchannel, msg, cb) {
if(typeof webchannel !== "undefined") {
var split = msg.indexOf(" ");
var peer;
if (split >= 0) {
command = msg.substr(0, split);
if(command === '/msg') {
msg = msg.substr(split+1);
var splitUser = msg.indexOf(" ");
var user = (splitUser >= 0) ? msg.substr(0, splitUser) : null;
var peer = findPeerById(user, webchannel);
if(peer) {
msg = msg.substr(splitUser+1);
peer.send(msg);
logMsg('' + peer.id + '<< ' + msg);
}
}
}
if(!peer) {
webchannel.send(msg);
}
cb();
}
else {
cb("Not connected to server");
}
};
var main = function () {
$backscroll = $('#chatflux-backscroll');
var $entry = $('#chatflux-entry');
connect((''+window.location.href).replace('http','ws').replace(/#.*$/, '')).then(function(wc){
$entry.on('keydown', function (evt) {
if (evt.keyCode !== 13) { return; }
send(wc, $entry.val(), function (err) {
if (err) {
logMsg('ERROR: ' + err);
return;
}
$entry.val('');
});
});
}, function(err) {
logMsg('ERROR: ' + err);
console.error(err);
});
};
main();
});