-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: similar to version 1.0.0
- Loading branch information
Showing
4 changed files
with
105 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,85 +1,73 @@ | ||
'use strict' | ||
|
||
let WebSocketServer = require('ws').Server | ||
let portfinder = require('portfinder') | ||
|
||
module.exports.start = () => { | ||
return new Promise((resolve, reject) => { | ||
let wss | ||
// portfinder looks for open port for WebSocket server | ||
portfinder.getPort((err, port) => { | ||
if (err) { | ||
console.log(err) | ||
return | ||
} | ||
wss = new WebSocketServer({port}, function () { | ||
console.log('Server run on: ws://localhost:' + port) | ||
resolve(wss) | ||
}) | ||
const PORT = 8000 | ||
|
||
wss.on('connection', (ws) => { | ||
ws.on('message', (message) => { | ||
let obj = JSON.parse(message) | ||
let wss = new WebSocketServer({port: PORT}, () => { | ||
console.log('Server runs on: ws://localhost:' + PORT) | ||
}) | ||
|
||
// Check income message format | ||
if (obj.hasOwnProperty('type')) { | ||
if (obj.type === 'icecandidate' || obj.type === 'offer') { | ||
// If 'index' property exists then this message comes from the peer | ||
// who triggered connection | ||
if (obj.hasOwnProperty('index')) { | ||
ws.peers[obj.index].send(message) | ||
} else { | ||
// Otherwise it comes from one of peers wishing to connect | ||
ws.peer.send(message) | ||
} | ||
return | ||
} else if (obj.type === 'open') { | ||
return _open(ws, obj.id) | ||
} else if (obj.type === 'join') { | ||
return _join(ws, obj.id) | ||
} | ||
} | ||
wss.on('connection', (ws) => { | ||
ws.on('message', (message) => { | ||
let obj = JSON.parse(message) | ||
|
||
// Close web socket if income message format is unknown | ||
ws.close() | ||
}) | ||
}) | ||
}) | ||
|
||
/** | ||
* Handles 'open' request from a client. | ||
* | ||
* @param {WebSocket} ws web socket of a peer who triggered connection | ||
* @param {string} id identifier sent by him | ||
* @return {void} | ||
*/ | ||
function _open (ws, id) { | ||
for (let i in wss.clients) { | ||
if (wss.clients[i].roomId === id) { | ||
ws.close() | ||
// Check income message format | ||
if (obj.hasOwnProperty('type')) { | ||
if (obj.type === 'icecandidate' || obj.type === 'offer') { | ||
// If 'index' property exists then this message comes from the peer | ||
// who triggered connection | ||
if (obj.hasOwnProperty('index')) { | ||
ws.peers[obj.index].send(message) | ||
} else { | ||
// Otherwise it comes from one of peers wishing to connect | ||
ws.peer.send(message) | ||
} | ||
return | ||
} else if (obj.type === 'open') { | ||
return _open(ws, obj.id) | ||
} else if (obj.type === 'join') { | ||
return _join(ws, obj.id) | ||
} | ||
ws.roomId = id | ||
ws.peers = [] | ||
} | ||
|
||
/** | ||
* Handles 'join' request from a client. | ||
* | ||
* @param {WebScoket} ws web socket of a peer wishing to connect | ||
* @param {string} id identifier of connection | ||
* @return {void} | ||
*/ | ||
function _join (ws, id) { | ||
for (let i in wss.clients) { | ||
if (wss.clients[i].roomId === id) { | ||
ws.peer = wss.clients[i] | ||
wss.clients[i].peers.push(ws) | ||
wss.clients[i].send('{"type":"join"}') | ||
return | ||
} | ||
} | ||
// Close web socket if income message format is unknown | ||
ws.close() | ||
}) | ||
}) | ||
|
||
/** | ||
* Handles 'open' request from a client. | ||
* | ||
* @param {WebSocket} ws web socket of a peer who triggered connection | ||
* @param {string} id identifier sent by him | ||
* @return {void} | ||
*/ | ||
function _open (ws, id) { | ||
for (let i in wss.clients) { | ||
if (wss.clients[i].roomId === id) { | ||
ws.close() | ||
} | ||
}) | ||
} | ||
ws.roomId = id | ||
ws.peers = [] | ||
} | ||
|
||
/** | ||
* Handles 'join' request from a client. | ||
* | ||
* @param {WebScoket} ws web socket of a peer wishing to connect | ||
* @param {string} id identifier of connection | ||
* @return {void} | ||
*/ | ||
function _join (ws, id) { | ||
for (let i in wss.clients) { | ||
if (wss.clients[i].roomId === id) { | ||
ws.peer = wss.clients[i] | ||
wss.clients[i].peers.push(ws) | ||
wss.clients[i].send('{"type":"join"}') | ||
return | ||
} | ||
} | ||
ws.close() | ||
} | ||
|
||
module.exports = wss |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,56 @@ | ||
'use strict' | ||
|
||
let sigver = require('./server') | ||
let wss = require('./server') | ||
let WebSocket = require('ws') | ||
|
||
sigver | ||
.start() | ||
.then((wss) => { | ||
let wsTrigger, wsJoining | ||
wsTrigger = new WebSocket('ws://localhost:' + wss.options.port) | ||
let wsTrigger, wsJoining | ||
wsTrigger = new WebSocket('ws://localhost:' + wss.options.port) | ||
|
||
wsTrigger.on('close', function open () { | ||
console.log('TRIGGER: web socket has been closed') | ||
wsTrigger.on('close', function open () { | ||
console.log('TRIGGER: web socket has been closed') | ||
}) | ||
wsTrigger.on('open', function open () { | ||
wsTrigger.on('message', function open (data, flags) { | ||
var msg = JSON.parse(data) | ||
if (msg.type === 'join') { | ||
console.log('TRIGGER: join received') | ||
wsTrigger.send(JSON.stringify({type: 'offer', index: 0, data: 'my offer'}), function () { | ||
console.log('TRIGGER: offer sent') | ||
}) | ||
} else if (msg.type === 'offer') { | ||
console.log('TRIGGER: offer received') | ||
wsTrigger.send(JSON.stringify({type: 'icecandidate', index: 0, data: 'my icecandidate'}), function () { | ||
console.log('TRIGGER: icecandidate sent') | ||
}) | ||
} else if (msg.type === 'icecandidate') { | ||
console.log('TRIGGER: icecandidate received') | ||
wss.close() | ||
} | ||
}) | ||
wsTrigger.send(JSON.stringify({type: 'open', id: '11111'}), function () { | ||
console.log('TRIGGER: open sent') | ||
wsJoining = new WebSocket('ws://localhost:' + wss.options.port + '/111111') | ||
wsJoining.on('close', function open () { | ||
console.log('JOINING: web socket has been closed') | ||
}) | ||
wsTrigger.on('open', function open () { | ||
wsTrigger.on('message', function open (data, flags) { | ||
wsJoining.on('open', function open () { | ||
wsJoining.on('message', function open (data, flags) { | ||
var msg = JSON.parse(data) | ||
if (msg.type === 'join') { | ||
console.log('TRIGGER: join received') | ||
wsTrigger.send(JSON.stringify({type: 'offer', index: 0, data: 'my offer'}), function () { | ||
console.log('TRIGGER: offer sent') | ||
}) | ||
} else if (msg.type === 'offer') { | ||
console.log('TRIGGER: offer received') | ||
wsTrigger.send(JSON.stringify({type: 'icecandidate', index: 0, data: 'my icecandidate'}), function () { | ||
console.log('TRIGGER: icecandidate sent') | ||
if (msg.type === 'offer') { | ||
console.log('JOINING: offer received') | ||
wsJoining.send(JSON.stringify({type: 'offer', data: 'and here is my offer'}), function () { | ||
console.log('JOINING: offer sent') | ||
}) | ||
} else if (msg.type === 'icecandidate') { | ||
console.log('TRIGGER: icecandidate received') | ||
wss.close() | ||
console.log('JOINING: icecandidate received') | ||
wsJoining.send(JSON.stringify({type: 'icecandidate', data: 'and here is my icecandidate'}), function () { | ||
console.log('JOINING: icecandidate sent') | ||
}) | ||
} | ||
}) | ||
wsTrigger.send(JSON.stringify({type: 'open', id: '11111'}), function () { | ||
console.log('TRIGGER: open sent') | ||
wsJoining = new WebSocket('ws://localhost:' + wss.options.port + '/111111') | ||
wsJoining.on('close', function open () { | ||
console.log('JOINING: web socket has been closed') | ||
}) | ||
wsJoining.on('open', function open () { | ||
wsJoining.on('message', function open (data, flags) { | ||
var msg = JSON.parse(data) | ||
if (msg.type === 'offer') { | ||
console.log('JOINING: offer received') | ||
wsJoining.send(JSON.stringify({type: 'offer', data: 'and here is my offer'}), function () { | ||
console.log('JOINING: offer sent') | ||
}) | ||
} else if (msg.type === 'icecandidate') { | ||
console.log('JOINING: icecandidate received') | ||
wsJoining.send(JSON.stringify({type: 'icecandidate', data: 'and here is my icecandidate'}), function () { | ||
console.log('JOINING: icecandidate sent') | ||
}) | ||
} | ||
}) | ||
wsJoining.send(JSON.stringify({type: 'join', id: '11111'}), function () { | ||
console.log('JOINING: join sent') | ||
}) | ||
}) | ||
wsJoining.send(JSON.stringify({type: 'join', id: '11111'}), function () { | ||
console.log('JOINING: join sent') | ||
}) | ||
}) | ||
}) | ||
}) |