R
R
Roman Alekseevich2018-03-21 15:18:54
Windows
Roman Alekseevich, 2018-03-21 15:18:54

Hangup of sending Socket.io to NODE under Windows - how to treat?

Good afternoon. I looked all over the Internet - I did not find the answer. There is a socket.io library for node. On linux it works as expected. But I work in windows (or rather, I study). And there is a small bug there. Not only is transport defined not as a websocket but as pooling, but this is not the problem. The problem is that when more than 1 client connects, sending messages via socket.io freezes for 10-20 seconds, and if you send it again, the new and previous ones pass instantly... Do you know this situation and if so, how to treat it?
my Server:

var io = require('socket.io');
var http = require('http');
var fs = require('fs');

var app = http .createServer(handler);
var io = io.listen(app);
app.listen('80');

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.on('eventtoServer', function (data) {
    console.log(data);
    io.sockets.emit('eventtoClient', { data: data.msg });
  });
  socket.on('disconnect', function () {
    console.log('user disconnected');
  });
});

my client:
<html>
<head>
  <title>Test socket.io</title>
  <script src="/socket.io/socket.io.js"></script>
  <script>
  var socket = io.connect('http://200.14.1.124:80');

  socket.on('eventtoClient', function (data) {
    document.getElementById("name").value = data.data;
    console.log(data);
  });
function send(){
var msgfromclient = document.getElementById("inp").value;
  socket.emit('eventtoServer', { msg: msgfromclient });
};

  </script>
</head>
<body>
  <textarea id="name" rows="8" cols="40"></textarea>
  <p></p>
  <input type="text" name="text" id="inp" size="20">
  <button type="button" name="button" onclick="send()">Отправить</button>
</body>
</html>

ps. Win10 system, on winServer12 the behavior is the same.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Alekseevich, 2018-03-21
@x2roma

You need to specify the socket type in the options.
const io = require('socket.io')(httpServer, { wsEngine: 'ws' });
This problem is only on Windows. Therefore, in the new version of socket.io 2.1.0, this setting will be the default.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question