{"id":2415,"date":"2015-09-19T12:02:59","date_gmt":"2015-09-19T17:02:59","guid":{"rendered":"http:\/\/www.jhclaura.com\/?p=2415"},"modified":"2015-09-19T16:28:22","modified_gmt":"2015-09-19T21:28:22","slug":"mask-v2-0-local-closed-network","status":"publish","type":"post","link":"http:\/\/www.jhclaura.com\/mask-v2-0-local-closed-network\/","title":{"rendered":"MASK v2.0 – Local server(*2) network!"},"content":{"rendered":"

\"Screen<\/a><\/p>\n

Quick notes on how I made\u00a0local Peer.js server, and also closed network!<\/p>\n

When making my thesis, I used cloud hosted PeerServer with API key, so the project was constrained by\u00a0internet access. In order to make the project more robust and stable, I decided to create PeerServer on my own laptop,\u00a0and let devices (mobile phones) access the\u00a0laptop (server) through Wi-Fi.<\/p>\n

Here are the quick notes on how I did those\u00a0(OMG such a googling \/ debugging hell!):<\/p>\n

 <\/p>\n

Create PeerServer accompanying\u00a0another\u00a0WebSocket system<\/span><\/strong><\/p>\n

Peer.js documented well about how to create your own PeerServer, even with examples\u00a0of combining with existing express app<\/a>, which is perfect for me. But because I have my own Node server as well, I struggled for a while to realize that, I should create two servers for each websocket with different ports!!! Finding\u00a0this out through non-stop console.log and try & error!<\/p>\n

Server codes<\/em><\/span><\/p>\n

var express = require('express');\r\nvar app = express();\r\nvar http = require('http');\r\nvar server = http.createServer(app);\r\nvar port = process.env.PORT || 7000;\r\n\r\n\/\/ peer_server\r\nvar ExpressPeerServer = require('peer').ExpressPeerServer;\r\nvar peerExpress = require('express');\r\nvar peerApp = peerExpress();\r\nvar peerServer = require('http').createServer(peerApp);\r\nvar options = { debug: true }\r\nvar peerPort = 9000;\r\n\r\napp.get('*', function(req, res){\r\n    res.sendFile(__dirname + req.url);\r\n});\r\npeerApp.use('\/pp', ExpressPeerServer(peerServer, options));\r\n\r\nserver.listen(port);\r\npeerServer.listen(peerPort);<\/pre>\n

 <\/p>\n

Client side\u00a0sudocode<\/em><\/span><\/p>\n

var peerConnect = function() {\t\t\t\r\n\t\/* WebRTC - Peer.js *\/\r\n\tpeer = new Peer( ID(), { secure: false, host: 'YOUR_IP', port: 5000, path:'\/pp', debug:true} );\r\n\r\n\t\/\/ Get an ID from the PeerJS server\t\t\r\n\tpeer.on('open', function(id) {\r\n\t\tconsole.log('My peer ID is: ' + id);\r\n\t\tpeer_id = id;\r\n\r\n\t\t\/\/ Now we can connect to my socket server\t\t\t\t\t\t\r\n\t\tconnectSocket();\r\n\t});\r\n\r\n\tpeer.on('call', function(incoming_call) {\r\n\t\tconsole.log("Got a call!");\r\n\t\tincoming_call.answer(my_stream); \/\/ Answer the call with an A\/V stream.\r\n\t\t\/\/ do what you want~~~\r\n\t});\r\n};<\/pre>\n

 <\/p>\n

MacbookPro as server, accessing by devices through Wi-Fi with router<\/strong><\/span><\/p>\n

    \n
  1. Connect Router with MacbookPro<\/li>\n
  2. Click Wi-Fi symbol to expand the list –> Create Network…\"Screen<\/a><\/li>\n
  3. Setup name and password\"Screen<\/a><\/li>\n
  4. System Preferences… –> Sharing\"Screen<\/a>\n