Skip to content

Commit

Permalink
refactor: run server on port 8000
Browse files Browse the repository at this point in the history
BREAKING CHANGE: similar to version 1.0.0
  • Loading branch information
kalitine committed Jan 19, 2016
1 parent fa16739 commit 454156d
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 128 deletions.
10 changes: 2 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,8 @@
Very simple signaling server based on WebSocket to test WebRTC.

## How to use
```javascript
require('sigver')
.start()
.then(function(webSocketServer) {
// Your code here
// For example to get server port
var port = webSocketServer.options.port
})
```
node server.js
```

## Message protocol
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"url": "https://github.com/coast-team/sigver.git"
},
"dependencies": {
"portfinder": "^0.4.0",
"ws": "^1.0.1"
},
"devDependencies": {
Expand Down
134 changes: 61 additions & 73 deletions server.js
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
88 changes: 42 additions & 46 deletions test.js
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')
})
})
})
})

0 comments on commit 454156d

Please sign in to comment.