|
8f600aa
jonmumm authored
|
||
| 1 | // socketapp.js | |
| 2 | // | |
| 3 | // This file handles all logic for generating and | |
| 4 | // distributing OpenTok sessions through sockets:w | |
| 5 | // | |
| 6 | ||
|
a29824d
Jonathan Mumm authored
|
||
| 7 | // Require and initialize OpenTok SDK | |
|
c1b2f76
Jonathan Mumm authored
|
||
| 8 | var opentok = require('opentok'); | |
|
162f985
Ubuntu authored
|
||
| 9 | var ot = new opentok.OpenTokSDK('8911642', 'b36521541e88f77d5dd6168c57426222f6dd8799', {API_URL: 'https://api.opentok.com/hl' }); | |
|
c1b2f76
Jonathan Mumm authored
|
||
| 10 | ||
|
a29824d
Jonathan Mumm authored
|
||
| 11 | // An array of users that do not have a chat partner | |
|
c1b2f76
Jonathan Mumm authored
|
||
| 12 | var soloUsers = []; | |
|
5139506
Jonathan Mumm authored
|
||
| 13 | var clients = {} | |
|
c1b2f76
Jonathan Mumm authored
|
||
| 14 | ||
|
a29824d
Jonathan Mumm authored
|
||
| 15 | // Sets up the socket server | |
|
5139506
Jonathan Mumm authored
|
||
| 16 | exports.start = function(sockets) { | |
| 17 | sockets.on('connection', function(socket) { | |
| 18 | clients[socket.id] = socket; | |
|
2e25fc4
Jonathan Mumm authored
|
||
| 19 | ||
| 20 | ot.createSession('localhost', {}, function(session) { | |
| 21 | ||
| 22 | // Each user should be a moderator | |
| 23 | var data = { | |
|
162f985
Ubuntu authored
|
||
| 24 | sessionId: session, | |
|
2e25fc4
Jonathan Mumm authored
|
||
| 25 | token: ot.generateToken({ | |
|
162f985
Ubuntu authored
|
||
| 26 | sessionId: session, | |
| 27 | role: opentok.RoleConstants.MODERATOR | |
|
2e25fc4
Jonathan Mumm authored
|
||
| 28 | }) | |
| 29 | }; | |
| 30 | ||
| 31 | // Send initialization data back to the client | |
|
5139506
Jonathan Mumm authored
|
||
| 32 | socket.emit('initial', data); | |
|
2e25fc4
Jonathan Mumm authored
|
||
| 33 | }); | |
| 34 | ||
|
50abdbf
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
Jonathan Mumm authored
|
||
| 62 | } | |
|
0c6bbbe
Jonathan Mumm authored
|
||
| 63 | } | |
|
50abdbf
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
Jonathan Mumm authored
|
||
| 110 | }); | |
|
5139506
Jonathan Mumm authored
|
||
| 111 | ||
|
8f600aa
jonmumm 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
Jonathan Mumm authored
|
||
| 124 | socket.on('disconnect', function() { | |
| 125 | delete clients[socket.id]; | |
| 126 | }); | |
|
2e25fc4
Jonathan Mumm authored
|
||
| 127 | }); | |
| 128 | }; |