googlecodelabs / webrtc-web

Realtime communication with WebRTC
https://codelabs.developers.google.com/codelabs/webrtc-web/
Apache License 2.0
754 stars 350 forks source link

socket.IO.listen is not a function #124

Open JeffreyJansen opened 3 years ago

JeffreyJansen commented 3 years ago

In step 7 the terminal command "node index.js" is followed by the error message: _TypeError: socketIO.listen is not a function at Object. (/Applications/MAMP/htdocs/webrtc-web-master/work/index.js:13:19). So the server doesn't start and I cannot see the result in the browser. I copied the code from the step 4 folder.

cody1991 commented 3 years ago

may be you use the latest version of socket, it not compatible with the old version

here my code,

const os = require('os');
const nodeStatic = require('node-static');
const http = require('http');
const { Server } = require('socket.io');

const fileServer = new nodeStatic.Server();
const app = http
  .createServer((req, res) => fileServer.serve(req, res))
  .listen(8000);

const io = new Server(app);
// io.on('connection', (socket) => {
//   console.log('a user connected');
// });

io.sockets.on('connection', (socket) => {
  console.log('a user connected');

  function log() {
    const array = ['Message from server:'];
    array.push.apply(array, arguments);
    socket.emit('log', array);
  }

  socket.on('message', function (message) {
    log('Client said: ', message);
    // for a real app, would be room-only (not broadcast)
    socket.broadcast.emit('message', message);
  });

  socket.on('create or join', (room) => {
    log('Received request to create or join room ' + room);

    const clientsInRoom = io.sockets.adapter.rooms.get(room);

    const numClients = clientsInRoom ? clientsInRoom.size : 0;

    log('Room ' + room + ' now has ' + numClients + ' client(s)');

    if (numClients === 0) {
      socket.join(room);
      log('Client ID ' + socket.id + ' created room ' + room);
      socket.emit('created', room, socket.id);
    } else if (numClients === 1) {
      log('Client ID ' + socket.id + ' joined room ' + room);
      io.sockets
        .in(room)
        .emit('join', 'Client ID ' + socket.id + ' joined room');
      socket.join(room);
      socket.emit('joined', room, socket.id);
      io.sockets.in(room).emit('ready', 'Client ID ' + socket.id + ' ready');
    } else {
      // max two clients
      socket.emit('full', room);
    }
  });

  socket.on('ipaddr', function () {
    const ifaces = os.networkInterfaces();
    for (const dev in ifaces) {
      ifaces[dev].forEach(function (details) {
        if (details.family === 'IPv4' && details.address !== '127.0.0.1') {
          socket.emit('ipaddr', details.address);
        }
      });
    }
  });

  socket.on('bye', function () {
    console.log('received bye');
  });
});

the method to create scoket io server is different

and the object: io.sockets.adapter.rooms is a Map now

texwerx commented 3 years ago

I’m on iPhone can I do it on phone

Sent from my iPhone

On May 20, 2021, at 9:38 PM, 懒熊吖 @.***> wrote:

 may be you use the latest version of socket, it not compatible with the old version

here my code,

const os = require('os'); const nodeStatic = require('node-static'); const http = require('http'); const { Server } = require('socket.io');

const fileServer = new nodeStatic.Server(); const app = http .createServer((req, res) => fileServer.serve(req, res)) .listen(8000);

const io = new Server(app); // io.on('connection', (socket) => { // console.log('a user connected'); // });

io.sockets.on('connection', (socket) => { console.log('a user connected');

function log() { const array = ['Message from server:']; array.push.apply(array, arguments); socket.emit('log', array); }

socket.on('message', function (message) { log('Client said: ', message); // for a real app, would be room-only (not broadcast) socket.broadcast.emit('message', message); });

socket.on('create or join', (room) => { log('Received request to create or join room ' + room);

const clientsInRoom = io.sockets.adapter.rooms.get(room);

const numClients = clientsInRoom ? clientsInRoom.size : 0;

log('Room ' + room + ' now has ' + numClients + ' client(s)');

if (numClients === 0) {
  socket.join(room);
  log('Client ID ' + socket.id + ' created room ' + room);
  socket.emit('created', room, socket.id);
} else if (numClients === 1) {
  log('Client ID ' + socket.id + ' joined room ' + room);
  io.sockets
    .in(room)
    .emit('join', 'Client ID ' + socket.id + ' joined room');
  socket.join(room);
  socket.emit('joined', room, socket.id);
  io.sockets.in(room).emit('ready', 'Client ID ' + socket.id + ' ready');
} else {
  // max two clients
  socket.emit('full', room);
}

});

socket.on('ipaddr', function () { const ifaces = os.networkInterfaces(); for (const dev in ifaces) { ifaces[dev].forEach(function (details) { if (details.family === 'IPv4' && details.address !== '127.0.0.1') { socket.emit('ipaddr', details.address); } }); } });

socket.on('bye', function () { console.log('received bye'); }); }); the method to create scoket io server is different

and the object: io.sockets.adapter.rooms is a Set now

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.

texwerx commented 3 years ago

Socket on AppStore hey you know about test flight app

Sent from my iPhone

On May 22, 2021, at 12:05 PM, Stephen Brady Jr. @.***> wrote:

I’m on iPhone can I do it on phone

Sent from my iPhone

On May 20, 2021, at 9:38 PM, 懒熊吖 @.***> wrote:

 may be you use the latest version of socket, it not compatible with the old version

here my code,

const os = require('os'); const nodeStatic = require('node-static'); const http = require('http'); const { Server } = require('socket.io');

const fileServer = new nodeStatic.Server(); const app = http .createServer((req, res) => fileServer.serve(req, res)) .listen(8000);

const io = new Server(app); // io.on('connection', (socket) => { // console.log('a user connected'); // });

io.sockets.on('connection', (socket) => { console.log('a user connected');

function log() { const array = ['Message from server:']; array.push.apply(array, arguments); socket.emit('log', array); }

socket.on('message', function (message) { log('Client said: ', message); // for a real app, would be room-only (not broadcast) socket.broadcast.emit('message', message); });

socket.on('create or join', (room) => { log('Received request to create or join room ' + room);

const clientsInRoom = io.sockets.adapter.rooms.get(room);

const numClients = clientsInRoom ? clientsInRoom.size : 0;

log('Room ' + room + ' now has ' + numClients + ' client(s)');

if (numClients === 0) {
  socket.join(room);
  log('Client ID ' + socket.id + ' created room ' + room);
  socket.emit('created', room, socket.id);
} else if (numClients === 1) {
  log('Client ID ' + socket.id + ' joined room ' + room);
  io.sockets
    .in(room)
    .emit('join', 'Client ID ' + socket.id + ' joined room');
  socket.join(room);
  socket.emit('joined', room, socket.id);
  io.sockets.in(room).emit('ready', 'Client ID ' + socket.id + ' ready');
} else {
  // max two clients
  socket.emit('full', room);
}

});

socket.on('ipaddr', function () { const ifaces = os.networkInterfaces(); for (const dev in ifaces) { ifaces[dev].forEach(function (details) { if (details.family === 'IPv4' && details.address !== '127.0.0.1') { socket.emit('ipaddr', details.address); } }); } });

socket.on('bye', function () { console.log('received bye'); }); }); the method to create scoket io server is different

and the object: io.sockets.adapter.rooms is a Set now

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or unsubscribe.