html - Socket.IO Websocket 发送消息不适用于 Firefox 和 Chrome

标签 html websocket socket.io

我有一个正在运行的带有 apache 和 Socket.IO 的服务器。我正在尝试在我的网站上使用 socket.io 发送和接收消息。

这是我的服务器的代码:

var fs = require('fs');
var hskey = fs.readFileSync('file.key');
var hscert = fs.readFileSync('file.crt');

var options = {
    key: hskey,
    cert: hscert
};

var app = require('https').createServer(options);
var io = require('/usr/local/lib/node_modules/socket.io').listen(app);

app.listen(8181);

io.sockets.on('connection', function (socket) {
  socket.emit('serverMessage', 'Bienvenue master!');
socket.broadcast.emit('serverMessage', 'New user online');
});

这是网页:

<!doctype html>
<html>
  <head>
    <title>Socket.io Test</title>
    <script src="./socket.io.js"></script>
  </head>
  <body>

    <script>

    var socket;
    var firstconnect = true;

    function connect() {
      if(firstconnect) {
        socket = io.connect('https://secure.mysite.com:8181');

        socket.on('serverMessage', function(data){ message(data); });
        socket.on('connect', function(){ status_update("Connected to Server"); });
        socket.on('disconnect', function(){ status_update("Disconnected from Server"); });
        socket.on('reconnect', function(){ status_update("Reconnected to Server"); });
        socket.on('reconnecting', function( nextRetry ){ status_update("Reconnecting in " 
          + nextRetry + " seconds"); });
        socket.on('reconnect_failed', function(){ message("Reconnect Failed"); });

        firstconnect = false;
      }
      else {
        socket.socket.reconnect();
      }
    }

    function disconnect() {
      socket.disconnect();
    }

    function message(data) {
      document.getElementById('message').innerHTML += "<br>" + "Server says: " + data;
    }

    function status_update(txt){
      document.getElementById('status').innerHTML = txt;
    }

    function esc(msg){
      return msg.replace(/</g, '&lt;').replace(/>/g, '&gt;');
    }

    function send() {
      socket.send('clientMessage', 'world');    
    };        

    </script>

    <h1>Socket.io Test</h1>
    <div><p id="status">Waiting for input</p></div>
    <div><p id="message"></p></div>  
    <button id="connect" onClick='connect()'/>Connect</button>
    <button id="disconnect" onClick='disconnect()'>Disconnect</button>
    <button id="send" onClick='send()'/>Send Message</button>
  </body>
</html>

在 Safari (websocket) 和 Opera (json pooling) 下一切似乎都工作正常,但在 Firefox 和 Chrome (websocket) 下我无法从客户端向服务器发送任何消息。其他一切都正常,我可以握手、连接并获取服务器消息。我做了很多研究,但似乎我是唯一遇到这个问题的人。

谢谢你帮助我!

最佳答案

我发现了问题,我使用的是与服务器端不同版本的socket.io.js。

关于html - Socket.IO Websocket 发送消息不适用于 Firefox 和 Chrome,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10050799/

相关文章:

node.js - SocketIo.use(function(socket, next)) - 下一步去哪里,如何捕获或接收它?

html - 使用 HTML 表格,如何使第一列固定宽度,第二列和第三列各占剩余宽度的 50%

python - 我在 "TypeError: str() takes at most 1 argument (2 given)"变量处收到此错误 "client_response"

web - 通过 websocket 将 kubernetes 日志暴露给浏览器

javascript - Socket IO 和动态房间

node.js - 将 socket.io 更新到 1.0 版本时出现多个轮询请求(HTTP 错误 400)

jquery - 根据文本量追加表列

javascript - 当我从 Pen 将它复制到 PHPStorm 时,为什么这个文本动画代码不起作用?

javascript - 附加 div 与表 thead th 元素内联

ajax - Websocket vs Ajax vs cometd