Local PeerJS Server disconnected after connected, and then connected again

2,010 views
Skip to first unread message

Laura Juo-Hsin Chen

unread,
Jul 13, 2015, 10:45:08 PM7/13/15
to pee...@googlegroups.com
Hi, 

I succeed in using PeerJS with cloud server to make video conference thing, along with the websocket library ws. Now I tried to setup a local PeerJS Server, following the instruction from Combining with existing express app here, but things acted weirdly. Below are my codes:

-- Server --
//SET_UP
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
var options = {
    debug: true
};
var http = require('http');
var server = http.createServer(app);
var port = process.env.PORT || 9000;

app.get('*', function(req, res){
res.sendFile(__dirname + req.url);
});
app.use('/peerjs', ExpressPeerServer(server, options));

server.listen(port);

//Websocket
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer( {'server': server} );

wss.on('connection', function(ws){
ws.on('message', function(data){
// ...
});

ws.on('close', function(){
// ...
});

});

-- Client --
peer = new Peer('123abc', { host: 'localhost', port: 9000, path:'/peerjs', debug:true});

// Get an ID from the PeerJS server
peer.on('open', function(id) {
console.log('My peer ID is: ' + id);
 
    // Now we can connect to the socket server
connectSocket();
});

And when I open the client side webpage http://localhost:9000/index.htmlI got those information and error in console:
PeerJS:  Socket open
My peer ID is: 123abc
WebSocket connection to 'ws://localhost:9000/peerjs/peerjs?key=peerjs&id=123abc&token=i1nay3ueh3tprpb9' failed: Invalid frame header
PeerJS:  Socket closed.
PeerJS:  ERROR Error: Lost connection to server.

It seems the PeerJS socket was connected once but shortly disconnect after. Couldn't figure out why. 
Please advice with any ideas and feedback. Thanks!

Laura

Laura Juo-Hsin Chen

unread,
Jul 13, 2015, 10:50:27 PM7/13/15
to pee...@googlegroups.com
Sorry about the Title, it's just Local PeerJS Server disconnected after connected. It didn't connect again. Sorry about the confused.

Laura Juo-Hsin Chen

unread,
Jul 20, 2015, 11:30:38 PM7/20/15
to pee...@googlegroups.com
Solved it! Should create two servers for each websocket with different port.
My codes might not be efficient, but it works.

var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var port = process.env.PORT || 7000;

// peer_server

var ExpressPeerServer = require('peer').ExpressPeerServer;
var peerExpress = require('express');
var peerApp = peerExpress();
var peerServer = require('http').createServer(peerApp);

var options = { debug: true }
var peerPort = 9000;


app
.get('*', function(req, res){
    res
.sendFile(__dirname + req.url);
});

peerApp
.use('/pp', ExpressPeerServer(peerServer, options));

server
.listen(port);
peerServer
.listen(peerPort);



On Monday, July 13, 2015 at 10:45:08 PM UTC-4, Laura Juo-Hsin Chen wrote:

Andrea Bisello

unread,
Mar 20, 2016, 4:21:15 PM3/20/16
to PeerJS
Hi Laura,
thanks,
please can you help me?

in cloud9 ide i set up a new node.js application.

there i installed node npm install peer

so i added server like you suggested

//peerjs
var ExpressPeerServer = require('peer').ExpressPeerServer;
var peerExpress = require('express');
var peerApp = peerExpress();
var peerServer = require('http').createServer(peerApp);

var options = { debug: true }
var peerPort = 9000;

peerApp.use('/pp', ExpressPeerServer(peerServer, options));
peerServer.listen(peerPort);

and application is running here http://thron-collaboration-abioneperhobby.c9users.io/ so i created my peer object like this

new Peer(this.id, {host: 'thron-collaboration-abioneperhobby.c9users.io', port: 9000, secure:false, path: '/pp'});


can you help me please?

thanks
Reply all
Reply to author
Forward
0 new messages