【问题标题】:WebSocket server can't receive message sended from ios clientWebSocket 服务器无法接收从 ios 客户端发送的消息
【发布时间】:2017-04-12 16:24:18
【问题描述】:

我使用ws 来实现WebSocket 服务器并且我成功地向我的ios 客户端发送消息(使用SocketRocket )。但是我无法向服务器发送消息,或者服务器可能无法接收从ios 客户端发送的消息。

WebSocket 服务器:

const WebSocket = require('ws');

const wss = new WebSocket.Server({port: 8080});

wss.on("connection", function connection(ws) {
    ws.on('message,', function incoming(message) {
        console.log('received: %s', message);
    });
    ws.send("something");
});

我想实现一个可以与ios客户端交换数据的websocket服务器。

【问题讨论】:

  • Websocket 服务器看起来太简单了,尝试添加更多事件监听器并使用console.log 输出更多信息。 iOS 客户端是否连接到服务器?
  • 是的,客户端收到消息:something.

标签: ios node.js websocket


【解决方案1】:

wss.on("connection", function connection(ws) { ws.on('message', function(message) { console.log('received: %s', message); }); ws.send("something"); });

查看回调 on('message',,您正在定义一个函数,而不是普通匿名函数用作回调也是 message 而不是 message,(注意逗号)。

尝试使用示例回显服务器以确保一切正常,使用您的 iOS 客户端并且您没有任何奇怪的问题,然后继续进行进一步的工作。

【讨论】:

    猜你喜欢
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 2018-10-25
    • 2011-05-12
    • 2015-10-16
    相关资源
    最近更新 更多