Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 129 lines (104 sloc) 3.519 kb
8f600aa Jonathan Mumm Changed the way partner sessions are disconnected because iOS does not h...
authored
1 // socketapp.js
2 //
3 // This file handles all logic for generating and
4 // distributing OpenTok sessions through sockets:w
5 //
6
a29824d Fixed inList var to update correctly
Jonathan Mumm authored
7 // Require and initialize OpenTok SDK
c1b2f76 init
Jonathan Mumm authored
8 var opentok = require('opentok');
9 var ot = new opentok.OpenTokSDK('413302', 'fc512f1f3c13e3ec3f590386c986842f92efa7e7');
10
a29824d Fixed inList var to update correctly
Jonathan Mumm authored
11 // An array of users that do not have a chat partner
c1b2f76 init
Jonathan Mumm authored
12 var soloUsers = [];
5139506 Fixed socket bugs
Jonathan Mumm authored
13 var clients = {}
c1b2f76 init
Jonathan Mumm authored
14
a29824d Fixed inList var to update correctly
Jonathan Mumm authored
15 // Sets up the socket server
5139506 Fixed socket bugs
Jonathan Mumm authored
16 exports.start = function(sockets) {
17 sockets.on('connection', function(socket) {
18 clients[socket.id] = socket;
2e25fc4 Updated socket.io to latest version
Jonathan Mumm authored
19
20 ot.createSession('localhost', {}, function(session) {
21
22 // Each user should be a moderator
23 var data = {
24 sessionId: session.sessionId,
25 token: ot.generateToken({
26 sessionId: session.sessionId,
27 role: opentok.Roles.MODERATOR
28 })
29 };
30
31 // Send initialization data back to the client
5139506 Fixed socket bugs
Jonathan Mumm authored
32 socket.emit('initial', data);
2e25fc4 Updated socket.io to latest version
Jonathan Mumm authored
33 });
34
50abdbf Changed socket transport back to emit api
Jonathan Mumm authored
35 socket.on('next', function (data) {
36 // Create a "user" data object for me
37 var me = {
38 sessionId: data.sessionId,
39 socketId: socket.id
40 };
41
42 var partner;
43 var partnerSocket;
44 // Look for a user to partner with in the list of solo users
45 for (var i = 0; i < soloUsers.length; i++) {
46 var tmpUser = soloUsers[i];
47
48 // Make sure our last partner is not our new partner
49 if (socket.partner != tmpUser) {
50 // Get the socket client for this user
51 partnerSocket = clients[tmpUser.socketId];
52
53 // Remove the partner we found from the list of solo users
54 soloUsers.splice(i, 1);
55
56 // If the user we found exists...
57 if (partnerSocket) {
58 // Set as our partner and quit the loop today
59 partner = tmpUser;
60 break;
61 }
bbb3d92 Added exception handling
Jonathan Mumm authored
62 }
0c6bbbe Changed the way socket messages are sent to server
Jonathan Mumm authored
63 }
50abdbf Changed socket transport back to emit api
Jonathan Mumm authored
64
65 // If we found a partner...
66 if (partner) {
67
68 // Tell myself to subscribe to my partner
69 socket.emit('subscribe', {
70 sessionId: partner.sessionId,
71 token: ot.generateToken({
72 sessionId: partner.sessionId,
73 role: opentok.Roles.SUBSCRIBER
74 })
75 });
76
77 // Tell my partner to subscribe to me
78 partnerSocket.emit('subscribe', {
79 sessionId: me.sessionId,
80 token: ot.generateToken({
81 sessionId: me.sessionId,
82 role: opentok.Roles.SUBSCRIBER
83 })
84 });
85
86 // Mark that my new partner and me are partners
87 socket.partner = partner;
88 partnerSocket.partner = me;
89
90 // Mark that we are not in the list of solo users anymore
91 socket.inlist = false;
92 partnerSocket.inlist = false;
93
94 } else {
95
96 // delete that i had a partner if i had one
97 if (socket.partner) {
98 delete socket.partner;
99 }
100
101 // add myself to list of solo users if i'm not in the list
102 if (!socket.inlist) {
103 socket.inlist = true;
104 soloUsers.push(me);
105 }
106
107 // tell myself that there is nobody to chat with right now
108 socket.emit('empty');
109 }
2e25fc4 Updated socket.io to latest version
Jonathan Mumm authored
110 });
5139506 Fixed socket bugs
Jonathan Mumm authored
111
8f600aa Jonathan Mumm Changed the way partner sessions are disconnected because iOS does not h...
authored
112 socket.on('disconnectPartners', function() {
113 if (socket.partner && socket.partner.socketId) {
114 var partnerSocket = clients[socket.partner.socketId]
115
116 if (partnerSocket) {
117 partnerSocket.emit('disconnectPartner');
118 }
119
120 socket.emit('disconnectPartner');
121 }
122 });
123
5139506 Fixed socket bugs
Jonathan Mumm authored
124 socket.on('disconnect', function() {
125 delete clients[socket.id];
126 });
2e25fc4 Updated socket.io to latest version
Jonathan Mumm authored
127 });
128 };
Something went wrong with that request. Please try again.