Node.js 和 socket.io

发布于 2024-12-11 17:35:33 字数 1672 浏览 0 评论 0原文

我创建了一个小型服务器,用于使用 node.js 和套接字 io 进行通信

服务器:

var express = require('/usr/local/lib/node_modules/express')
var io = require('/usr/local/lib/node_modules/socket.io');
var server = express.createServer();
server.listen(8888,'localhost');
io = io.listen(server);
io.of("namespace").on('connection', function(client){
     client.emit("message",'connection successful');
     client.on('message', function(m){
        console.log("received message"+m);
         client.broadcast(m);
     });
});

客户端:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Comet Test</title>
    <script src="http://localhost:8888/socket.io/socket.io.js" type="text/javascript"></script>
</head>
<body>
    <p><a id='customAlert' href="#" onclick='sendMessage("customAlert")'>publish customAlert</a></p>
    <p><a id='customAlert2' href="#" onclick='sendMessage("customAlert2")'>publish customAl
    <script type="text/javascript">
     // Start the socket
      var socket = io.connect('http://localhost:8888/namespace');
      socket.on('error', function (reason){
        console.error('Unable to connect Socket.IO', reason);
      });
      socket.on('message', function(msg){
          console.log(msg);
      });
      function sendMessage(m) {
        console.log("sending message"+m);   
        socket.emit("message",m);
      }
    </script>
</body>

沟通没有发生,我在这里做错了什么吗?

I have created a small server for communication using node.js and socket io

Server:

var express = require('/usr/local/lib/node_modules/express')
var io = require('/usr/local/lib/node_modules/socket.io');
var server = express.createServer();
server.listen(8888,'localhost');
io = io.listen(server);
io.of("namespace").on('connection', function(client){
     client.emit("message",'connection successful');
     client.on('message', function(m){
        console.log("received message"+m);
         client.broadcast(m);
     });
});

Client:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Comet Test</title>
    <script src="http://localhost:8888/socket.io/socket.io.js" type="text/javascript"></script>
</head>
<body>
    <p><a id='customAlert' href="#" onclick='sendMessage("customAlert")'>publish customAlert</a></p>
    <p><a id='customAlert2' href="#" onclick='sendMessage("customAlert2")'>publish customAl
    <script type="text/javascript">
     // Start the socket
      var socket = io.connect('http://localhost:8888/namespace');
      socket.on('error', function (reason){
        console.error('Unable to connect Socket.IO', reason);
      });
      socket.on('message', function(msg){
          console.log(msg);
      });
      function sendMessage(m) {
        console.log("sending message"+m);   
        socket.emit("message",m);
      }
    </script>
</body>

Communication does not happen, am I doing something wrong here?

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

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

发布评论

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

评论(1

油饼 2024-12-18 17:35:33

如果您的服务器正在侦听端口 8888,则需要客户端连接到端口 8888 上的服务器。

客户端代码中的以下行:

var socket = io.connect('http://localhost/namespace');

应读取:

 var socket = io.connect('http://localhost:8888/namespace');

If your server is listening on port 8888, you need the client to connect to the server on port 8888.

The following line in your client code:

var socket = io.connect('http://localhost/namespace');

Should read:

 var socket = io.connect('http://localhost:8888/namespace');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文