socket.io多行消息问题

时间:2015-05-23 20:30:01

标签: node.js socket.io

我们使用socket.io@1.3.5和Socket.IO-Client-Swift来实现聊天应用程序。当发送一行消息时,一切正常。 socket.io-parser解析数据包并对其进行处理。

one line message

  engine handling "POST" http request "/socket.io/?transport=polling&sid=D4hHosMaFHKF2xZAAAAA" +0ms
  engine setting new request for existing client +1ms!
  engine:polling received "20:42["message","test"]" +0ms
  engine:socket packet +0ms
  socket.io-parser decoded 2["message","test"] as {"type":2,"nsp":"/","data":["message","test"]} +8.2m
  socket.io:socket got packet {"type":2,"nsp":"/","data":["message","test"]} +6m
  socket.io:socket emitting event ["message","test"] +0ms

但是当我尝试发送两行消息时,它无法对其进行解码;

two line message

  engine handling "POST" http request "/socket.io/?transport=polling&sid=D4hHosMaFHKF2xZAAAAA" +0ms
  engine setting new request for existing client +0ms
  engine:polling received "27:42["message","test2
test3"]" +1ms
  engine:socket packet +0ms
  socket.io:client no socket for namespace undefined +1m

该问题的可能解决方案是什么?

谢谢

修改:

如果超过255个字符

,我会修剪从输入框中获得的文本
if (text.length > 255) { // trim the text longer than 255
    text = [text substringToIndex:255];
}

然后我发出一条"消息"事件;

[_socket emit:@"message" withItems:@[text]];

在NSLog中似乎如下;

2015-05-23 23:36:43.580 TestApp[383:3300397] Text: test3
test4

我不是删除新行或任何其他特殊字符(我应该这样做)

编辑2:

我已经检查了我的旧配置Socket V0.9,看起来客户端发送消息为" test1 \ ntest2",我不确定问题是否属于客户端或服务器。它与标题内容类型有关吗?

1 个答案:

答案 0 :(得分:0)

通过添加" \ r \ n"解决问题手动字符。

显然我正在使用的旧实现(https://github.com/pkyeck/socket.IO-objc)是自动执行的。但是必须在代码中完成的新实现(https://github.com/nuclearace/Socket.IO-Client-Swift)。