Socket.io 服务器似乎没有连接?

发布于 2024-11-11 05:17:01 字数 2174 浏览 2 评论 0原文

我正在学习 NodeTuts 上的教程。我正在尝试使用 socket.io 让聊天服务器正常工作。

我可以正常启动节点实例,并且在启动时收到“socket.io 准备就绪 - 接受连接”消息。当我将浏览器指向 localhost:8888 时,我也会从 template.html 获得 html 表单,这样一切看起来都不错。不过,我似乎从未建立过套接字连接,也从未看到“已收到连接”日志或“欢迎来到聊天服务器”消息。

有什么想法吗?

var fs, http, io, server, socket, sys;
  http = require('http');
  fs = require('fs');
  sys = require('sys');
  io = require('socket.io');
  server = http.createServer(function(req, res) {
    var rs;
    console.log('server started');
    res.writeHead(200, {
      'Content-Type': 'text/html'
    });
    rs = fs.createReadStream(__dirname + '/template.html');
    return sys.pump(rs, res);
  });
  socket = io.listen(server);
  socket.on('connection', function(client) {
    var username;
    console.log('Connection received');
    client.send('Welcome to the chat server');
    client.send('Please enter a username');
    return client.on('message', function(message) {
      if (!username) {
        username = message;
        return client.send("Hi " + username + "!");
      } else {
        return socket.broadcast("" + username + ": " + message);
      }
    });
  });
  server.listen(8888);

这是template.html的JS部分

<script src="/socket.io/socket.io.js"></script>
<script>
    $(function() {
      socket = new io.Socket('localhost', {port: 8888});
      socket.on('message', function(message) {
        var data = message.data
        data = data.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
        console.log( data );
        $('#log ul').append('<li>' + data + '</li>');
        window.scrollBy(0, 1000000000000000);
        entry_el.focus();
      });


      var entry_el = $('#entry');
      entry_el.keypress(function(event) {
        if (event.keyCode != 13) return;
        var msg = entry_el.attr('value');
        // if the message is not empty
        if (msg) {
            socket.send(msg);
            entry_el.attr('value', '');
        }
      });
    });
  </script>

I'm working my way through the tutorials on NodeTuts. I'm trying to get a chat server working using socket.io.

I can start the node instance fine and I get the "socket.io ready - accepting connections" message when I do. When I point my browser at localhost:8888 I get served the html form from template.html also so that all looks good. I never seem to make a socket connection though and I never see the "Connection Received" log or the 'Welcome to the chat server' message.

Any ideas what the problem is?

var fs, http, io, server, socket, sys;
  http = require('http');
  fs = require('fs');
  sys = require('sys');
  io = require('socket.io');
  server = http.createServer(function(req, res) {
    var rs;
    console.log('server started');
    res.writeHead(200, {
      'Content-Type': 'text/html'
    });
    rs = fs.createReadStream(__dirname + '/template.html');
    return sys.pump(rs, res);
  });
  socket = io.listen(server);
  socket.on('connection', function(client) {
    var username;
    console.log('Connection received');
    client.send('Welcome to the chat server');
    client.send('Please enter a username');
    return client.on('message', function(message) {
      if (!username) {
        username = message;
        return client.send("Hi " + username + "!");
      } else {
        return socket.broadcast("" + username + ": " + message);
      }
    });
  });
  server.listen(8888);

Here's the JS part of template.html

<script src="/socket.io/socket.io.js"></script>
<script>
    $(function() {
      socket = new io.Socket('localhost', {port: 8888});
      socket.on('message', function(message) {
        var data = message.data
        data = data.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
        console.log( data );
        $('#log ul').append('<li>' + data + '</li>');
        window.scrollBy(0, 1000000000000000);
        entry_el.focus();
      });


      var entry_el = $('#entry');
      entry_el.keypress(function(event) {
        if (event.keyCode != 13) return;
        var msg = entry_el.attr('value');
        // if the message is not empty
        if (msg) {
            socket.send(msg);
            entry_el.attr('value', '');
        }
      });
    });
  </script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不气馁 2024-11-18 05:17:01

我相信您需要在客户端代码中调用 socket.connect(); ,紧接在 socket = new io.Socket('localhost', {port: 8888});< /代码>

I believe you need to call socket.connect(); in your client-side code, right after socket = new io.Socket('localhost', {port: 8888});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文