Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

socket.IO.listen is not a function #124

Open
JeffreyJansen opened this issue Mar 22, 2021 · 3 comments
Open

socket.IO.listen is not a function #124

JeffreyJansen opened this issue Mar 22, 2021 · 3 comments

Comments

@JeffreyJansen
Copy link

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
Copy link

cody1991 commented May 21, 2021

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
Copy link

texwerx commented May 22, 2021 via email

@texwerx
Copy link

texwerx commented May 22, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants