implementing 'orders' on peerjs

126 views
Skip to first unread message

André Dias

unread,
Jul 3, 2015, 6:09:53 AM7/3/15
to pee...@googlegroups.com
Hi, for my specific use case I require communication between 2 peers solely for signaling, yes this could be achieved with a data channel but the effort it takes to create a data channel and maintaining it is unnecessary for a simple message for custom app-signaling, not to mention is unnecessarily stresses the user.

So what I decided to implement 'orders', the idea is that a peer order another peer, triggering an event without establishing a data channel, the server is only required to send a message from one to the other, through it's active websockets

Implementation.

server: 
added 'ORDER' message type to the switch
*still trying to find out why sockets after 2 min of inactivity, might fiddle with a heartbeat

client

//added to message type switch
 case 'ORDER': //order given by the server
this.emit('order', message.subType, message.data);//just condensing the custom events inside the order handler, for clarity
break;

//the hard part

  Peer.prototype.order = function(dst,subType, data) {//dst is the peer id of the ordered peer, subType is order event to be triggered
  if (this.disconnected) {
      util.warn('You cannot order a Peer because you called ' +
      '.disconnect() on this Peer and ended your connection with the ' +
      'server. You can create a new Peer to reconnect.');
      this.emitError('disconnected', 'Cannot connect to server after disconnecting from it.');
      return;
  }
  if (!dst) {
    util.error('To order, please provide an online recipients id');
    return;
  }
  var order;
  order.dst= dst;
  order.data = data;
  order.type: 'ORDER';
  order.subType: subType;
  this.socket.send(order){}, function(err) {
  util.log('Failed to send order, ', err);
      return;
  };
};


Gonna start testing in a bit, just wanted to know if I'm making any glaring mistake and/or if there is interest in this feature.
Also any ideas why my websockets keeps closing every 2 min of inactivity?
lastly where could I place an error for dst not connected?
(disclaimer: university student in an internship working in a webrtc based webapp, learnt javascript a fortnight ago...)
Message has been deleted

André Dias

unread,
Jul 3, 2015, 12:44:57 PM7/3/15
to pee...@googlegroups.com
  Peer.prototype.order = function(dst,subType, data) {//dst is the peer id of the ordered peer, subType is order event to be triggered
  if (this.disconnected) {
      util.warn('You cannot order a Peer because you called ' +
      '.disconnect() on this Peer and ended your connection with the ' +
      'server. You can create a new Peer to reconnect.');
      this.emitError('disconnected', 'Cannot connect to server after disconnecting from it.');
      return;
  }
  if (!dst) {
    util.error('To order, please provide an online recipients id');
    return;
  }
  var order = {
  dst: dst,
  data: data,
  type: 'ORDER',
  subType: subType
  };
  this.socket.send(order);
};

Fixed a couple of syntax error.
Now I'm stuck, after digging around some more I'm hitting this ->https://github.com/peers/peerjs-server/issues/49
On firefox I'm hitting a CORS error, on chrome, it fails to connect the websocket

André Dias

unread,
Jul 6, 2015, 12:36:19 PM7/6/15
to pee...@googlegroups.com
Got it working.
//message type switch case

    case 'ORDER': //order given by the server
this.emit('order', message.data.subType, message.data.data);
break;

//order command
  Peer.prototype.order = function(dst,subType, data) {//dst is the peer id of the ordered peer, subType is order event to be triggered
  if (this.disconnected) {
      util.warn('You cannot order a Peer because you called ' +
      '.disconnect() on this Peer and ended your connection with the ' +
      'server. You can create a new Peer to reconnect.');
      this.emitError('disconnected', 'Cannot connect to server after disconnecting from it.');
      return;
  }
  if (!dst) {
    util.error('To order, please provide an online recipients id');
    return;
  }
  var order = {
  dst: dst,
  data: { subType: subType, data:data},
  type: 'ORDER',
  payload: ''
  };
  this.socket.send(order);
  util.log('order sent of subtype: ' +  subType + ' to: ' + dst + ' data:' + data);
};

With this code I managed to 'order' a user to establish a call to another one.
//app, user-side code
peer.on('order', function (subType, data){
switch( subType){
case 'call' :
var call = peer.call(data, window.localStream);
step3(call);//adds on stream handler
console.log("received call: ", Date());
break;
case 'end_call'://untested yet
window.existingCall.close();
step2();
break;
}
console.log('order received, of subtype' + subType);
var call = peer.call($('#callto-id').val(), window.localStream);
});


However I only managed to get my peer server to work by disabling Same Origin Policy from chrome, otherwise the server isn't answering to the the websocket upgrade request. 
Reply all
Reply to author
Forward
0 new messages