【问题标题】:socket.io client can not communicate to socket server at different portsocket.io 客户端无法与不同端口的套​​接字服务器通信
【发布时间】:2016-10-27 00:19:23
【问题描述】:

我正在学习 nodejs 使用 socket.io 的事件驱动编程,以便我可以构建一个聊天应用程序。

所以,我创建了一个ServerClient,现在在localhost 中运行在不同的端口。

8080 的服务器

var express = require('express');
var app = express();
var http = require('http').createServer(app);
var io = require('socket.io')(http, {origins: '*.*', transports: ['websocket', 'xhr-polling']});
var port = process.env.PORT || 8080;

http.listen(port, function () {
    console.log('Cat Server listening at http://127.0.0.1:%d', port);
});

app.use(express.static(__dirname + '/public'));

io.on('connection', function (socket) {

    console.log('Connection to client established ', socket);

    var addedUser = false;

    // when the client emits 'love event', this listens and executes
    socket.on('LoveEvent', function (data) {
        console.log("we tell the client to execute 'logevent'")
        socket.broadcast.emit('LoveEvent', {
            username: socket.username,
            message: data
        });
    });
);

客户在 3000

客户端通过socket.io连接到服务器8080

我在public/chat.js 中有以下代码,它包含在public/index.html

  var socket = io();
  socket.connect('http://localhost:8080', {transports: ['websocket', 'xhr-polling']})
  console.log("Connection to Cat socket server, ", socket)

而且,public/index.html

  <script src="http://localhost:8080/socket.io/socket.io.js"></script>
  <script src="/chat.js"></script>

客户端结构是

$ ll
total 24
-rw-r--r--   1 prayagupd  staff    30 Nov  1 20:18 README.md
-rw-r--r--   1 prayagupd  staff   325 Nov  1 20:18 Server.js
drwxr-xr-x  91 prayagupd  staff  3094 Oct 26 18:11 node_modules
-rw-r--r--   1 prayagupd  staff   249 Nov  1 20:18 package.json
drwxr-xr-x   8 prayagupd  staff   272 Nov  1 20:22 public

node_modulessocket.js

$ ll node_modules/socket.io/lib/
total 80
-rw-r--r--  1 prayagupd  staff  5411 Oct 26 15:59 client.js
-rw-r--r--  1 prayagupd  staff  8933 Oct 26 15:59 index.js
-rw-r--r--  1 prayagupd  staff  5366 Oct 26 15:59 namespace.js
-rw-r--r--  1 prayagupd  staff  9493 Oct 26 15:59 socket.js

但是

当我启动我的服务器时

$ node Server.js 
Cat Server listening at http://127.0.0.1:8080

和客户,

$ node Client.js 
Cat client running at localhost:3000

我希望在服务器端收到一条连接消息,因为我正在使用 Server.js 中的消息作为 console.log('Connection to client established ', socket); 进行调试

我手动尝试从 chorme 控制台创建 socket 并发送了一个事件,服务器似乎没有订阅它。

s.connect("http://localhost:8080", {autoConnect : true})
Socket {io: Manager, nsp: "/", json: Socket, ids: 0, acks: Object…}
s.emit("LoveEvent", "Love")
Socket {io: Manager, nsp: "/", json: Socket, ids: 0, acks: Object…}

此外,当客户端在浏览器中时,在浏览器控制台中的transport=polling 上有以下404 error

我不知道它为什么在 3000 端口上轮询,它本身就是。

当一切都在同一台服务器上时,它运行良好。我重构了它,现在:(

我这里有后端和前端代码 => prayagupd/lovejs

【问题讨论】:

    标签: node.js websocket socket.io


    【解决方案1】:

    当客户端和 socket.io 服务器分开时,您应该下载并复制 socket.io 客户端库(socket.io.jshttps://github.com/socketio/socket.io-client)到客户端应用程序的“socket.io”子文件夹中(例如) 然后像这样导入它:

    &lt;script src="/socket.io/socket.io.js"&gt;&lt;/script&gt;

    或者不复制,直接从CDN导入即可:

    &lt;script src="https://cdn.socket.io/socket.io-1.4.5.js"&gt;&lt;/script&gt;

    (没有互联网连接将无法工作)。

    而且你可以从服务端移除socket.io路由,在这种情况下是没用的:

    io = require('socket.io')({ serveClient: false, ...});

    【讨论】:

    • 那么在package.json 中添加依赖"socket.io" : "^1.5.1" 有什么意义??让我看看你的想法是否可行。
    【解决方案2】:

    也许是您的来源*.**:*how to set origins 的格式,但 socket.io 甚至可以在不同的端口上工作。

    【讨论】:

    • 在我的服务器上,我使用的是*.*,你可以在var io = require('socket.io')(http, {origins: '*.*', transports: ['websocket', 'xhr-polling']}); 行看到
    【解决方案3】:

    这也发生在我身上, 但我从 socket.io 版本 3 更改为 2 => "socket.io": "^2.3.0" 它解决了这个问题。

    【讨论】:

    • 您可以使用 ` `(反引号)格式化您的答案
    猜你喜欢
    • 2011-08-18
    • 2022-09-28
    • 1970-01-01
    • 2012-08-30
    • 2012-11-02
    • 1970-01-01
    • 2017-07-16
    • 1970-01-01
    • 2017-08-23
    相关资源
    最近更新 更多