PeerJS - 连接事件的参数不是id

时间:2016-06-17 03:30:16

标签: node.js webrtc peerjs

PeerJS Server中,连接事件的参数不是id。我想这是一个插座。

以下是我的代码。

var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;

app.get('/', function(req, res, next) { res.send('Hello world!'); });

var server = app.listen(9000);

var options = {
    debug: true
};

app.use('/api', ExpressPeerServer(server, options));

server.on('connection', function(id) { console.log(id); });

当我运行它并输入http://localhost:9000/时,控制台会打印大量文本,看起来不像是id。

Socket {
  _connecting: false,
  _hadError: false,
  _handle:
   TCP {
     _externalStream: {},
     fd: -1,
     reading: true,
     owner: [Circular],
     onread: [Function: onread],
     onconnection: null,
     writeQueueSize: 0 },
  _parent: null,
  _host: null,
  _readableState:
   ReadableState {
     objectMode: false,
     highWaterMark: 16384,
     buffer: [],
     length: 0,
     pipes: null,
     pipesCount: 0,
     flowing: true,
     ended: false,
     endEmitted: false,
     reading: true,
     sync: false,
     needReadable: true,
     emittedReadable: false,
     readableListening: false,
     resumeScheduled: true,
     defaultEncoding: 'utf8',
     ranOut: false,
     awaitDrain: 0,
     readingMore: false,
     decoder: null,
     encoding: null },
  readable: true,
  domain: null,
  _events:
   { end: [ [Object], [Function: socketOnEnd] ],
     finish: [Function: onSocketFinish],
     _socketEnd: [Function: onSocketEnd],
     drain: [ [Function: ondrain], [Function: socketOnDrain] ],
     timeout: [Function],
     error: [Function: socketOnError],
     close: [Function: serverSocketCloseListener],
     data: [Function: socketOnData],
     resume: [Function: onSocketResume],
     pause: [Function: onSocketPause] },
  _eventsCount: 10,
  _maxListeners: undefined,
  _writableState:
   WritableState {
     objectMode: false,
     highWaterMark: 16384,
     needDrain: false,
     ending: false,
     ended: false,
     finished: false,
     decodeStrings: false,
     defaultEncoding: 'utf8',
     length: 0,
     writing: false,
     corked: 0,
     sync: true,
     bufferProcessing: false,
     onwrite: [Function],
     writecb: null,
     writelen: 0,
     bufferedRequest: null,
     lastBufferedRequest: null,
     pendingcb: 0,
     prefinished: false,
     errorEmitted: false,
     bufferedRequestCount: 0,
     corkedRequestsFree: CorkedRequest { next: [Object], entry: null, finish: [Function] } },
  writable: true,
  allowHalfOpen: true,
  destroyed: false,
  bytesRead: 0,
  _bytesDispatched: 0,
  _sockname: null,
  _pendingData: null,
  _pendingEncoding: '',
  server:
   Server {
     domain: null,
     _events:
      { request: [Object],
        connection: [Object],
        clientError: [Function],
        error: [Function],
        upgrade: [Function] },
     _eventsCount: 5,
     _maxListeners: undefined,
     _connections: 1,
     _handle:
      TCP {
        _externalStream: {},
        fd: -1,
        reading: false,
        owner: [Circular],
        onread: null,
        onconnection: [Function: onconnection],
        writeQueueSize: 0 },
     _usingSlaves: false,
     _slaves: [],
     _unref: false,
     allowHalfOpen: true,
     pauseOnConnect: false,
     httpAllowHalfOpen: false,
     timeout: 120000,
     _pendingResponseData: 0,
     _connectionKey: '6::::9000',
     _webSocketPaths: { '/api/peerjs': 1 } },
  _server:
   Server {
     domain: null,
     _events:
      { request: [Object],
        connection: [Object],
        clientError: [Function],
        error: [Function],
        upgrade: [Function] },
     _eventsCount: 5,
     _maxListeners: undefined,
     _connections: 1,
     _handle:
      TCP {
        _externalStream: {},
        fd: -1,
        reading: false,
        owner: [Circular],
        onread: null,
        onconnection: [Function: onconnection],
        writeQueueSize: 0 },
     _usingSlaves: false,
     _slaves: [],
     _unref: false,
     allowHalfOpen: true,
     pauseOnConnect: false,
     httpAllowHalfOpen: false,
     timeout: 120000,
     _pendingResponseData: 0,
     _connectionKey: '6::::9000',
     _webSocketPaths: { '/api/peerjs': 1 } },
  _idleTimeout: 120000,
  _idleNext: { _idleNext: [Circular], _idlePrev: [Circular] },
  _idlePrev: { _idleNext: [Circular], _idlePrev: [Circular] },
  _idleStart: 2700,
  parser:
   HTTPParser {
     '0': [Function: parserOnHeaders],
     '1': [Function: parserOnHeadersComplete],
     '2': [Function: parserOnBody],
     '3': [Function: parserOnMessageComplete],
     '4': [Function: onParserExecute],
     _headers: [],
     _url: '',
     _consumed: true,
     socket: [Circular],
     incoming: null,
     outgoing: null,
     maxHeaderPairs: 2000,
     onIncoming: [Function: parserOnIncoming] },
  on: [Function: socketOnWrap],
  _paused: false }

如何获得香草ID?

1 个答案:

答案 0 :(得分:1)

您尝试使用http模块的连接事件,而不是ExpressPeerServer。

以下是经过修改的代码。

var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;

app.get('/', function(req, res, next) { res.send('Hello world!'); });

var server = app.listen(9000);

var options = {
    debug: true
};

var peerServer = ExpressPeerServer(server, options);
app.use('/api', peerServer);

peerServer.on('connection', function(id) { console.log(id); });

当您输入http://localhost:9000/时,它会打印香草ID。