2017-04-12 430 views
0

我使用ws来实现WebSocket服务器,并且我成功发送消息给我的ios客户端(使用SocketRocket )。但是我无法发送消息到服务器或者服务器不能接收来自ios客户端的消息。WebSocket服务器无法接收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服务器。

+0

Websocket服务器看起来过于简单,请尝试添加更多事件侦听器并使用console.log输出更多信息。 iOS客户端连接到服务器? – Gntem

+0

是的,客户端收到消息:某事。 – potato

回答

0

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

看回调on('message',要定义函数的不是一个简单的匿名函数用作回调也message而不是message,(注意逗号)。

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

相关问题