JavaScript WebSocket 字符编码问题(文本)



>我正在向我的 WebSocket 服务器发送一些数据,这有时会给我一些奇怪的字符,通常由最后一个"引起(可能是巧合,它总是最后一个(。在这些字符之间放置一个空格可以解决问题。但是,我宁愿正确处理,也不愿依赖我的客户端实现此空间(以防万一还有其他组合可能导致不需要的数据(。

我在 websocket 连接中发送的 JSON 字符串的示例如下:

{
    "action": "query",
    "tag": "test",
    "sql": "SELECT * FROM settings WHERE param = 'test'"
}

Websocket 服务器接收:

{
"action": "query",
"tag": "test",
"sql": "SELECT * FROM settings WHERE param = 'test'"b}

我也尝试过作为单行数据发送,但这并不能解决问题。其中一个客户端是用 JavaScript 编写的,另一个是用 C++ 编写的,两者都有同样的问题。根据单引号中的内容,我得到不同的结果。

我的测试 JavaScript 客户端的代码如下:

var socket = new WebSocket("ws://localhost:3399");
socket.onmessage = function (e) {
    console.log(e.data);
    document.querySelector("#response").innerHTML = e.data;
};
function sendMessage() {
    var ws_message = document.querySelector("#ws_message").value;
    socket.send(ws_message);
}

而 Node.js websocket 服务器:

connections[connection_id].on('message', function (message) {
    if (message.type === 'utf8') { // accept only text
        try {
            var args = JSON.parse(message.utf8Data);
            handleActions(args, connection_id);
        } catch (e) {
            serverLog(connection_id, "Message not formatted correctly");
        }
    }
});

任何帮助将不胜感激!

编辑:每次我发送它时它都会改变...

{
    "action": "query",
    "tag": "test",
    "sql": "SELECT * FROM settings WHERE param = 'fullscreen'"
R 

{
    "action": "query",
    "tag": "test",
    "sql": "SELECT * FROM settings WHERE param = 'fullscreen'"
5 

编辑2:即使在客户端上创建数据数组并使用JSON.stringify也会导致相同的问题。

服务器日志:

[1/12/2014 13:57:39] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen��}
[1/12/2014 13:57:39] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:44] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen��}
[1/12/2014 13:57:44] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:46] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen��}
[1/12/2014 13:57:46] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:47] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen�P}
[1/12/2014 13:57:47] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:48] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen�}
[1/12/2014 13:57:48] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:48] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen�}
[1/12/2014 13:57:48] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:49] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen��}
[1/12/2014 13:57:49] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:49] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen�/}
[1/12/2014 13:57:49] (conn: 1) Message not formatted correctly
[1/12/2014 13:57:50] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM `settings` WHERE param = 'fullscreen�S}
[1/12/2014 13:57:50] (conn: 1) Message not formatted correctly

编辑:缓冲区使用日志

81daf101eb988a238afb856884f6d33bc9e9846499e1d32dc9ec9066c9a2d3758eeb8523c7ba827087bacb23b8ddbd44a8ccd12bcbdea34ea6b882649fec986f8cebd156a3dda344cbe890738af5d13ccbbf977487f4826299fd946fcca3d37c
[2/12/2014 07:52:45] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM settings WHERE param = 'fullscreen';?}
[2/12/2014 07:52:45] (conn: 1) Message not formatted correctly
81daf3487331886a125287211c5fd1725140862d0148d1645145922f510bd13c1642876a5f1380391f13c96a2074bf0d3065d3625377a1073e11802d07459a261442d31f3b74a10d5341923a125cd3755316953d1f5d802b01549626540ad135
[2/12/2014 07:52:57] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM settings WHERE param = 'fullscreen';?}
[2/12/2014 07:52:57] (conn: 1) Message not formatted correctly
81da9a516309e173026aee380c67b86b4178ef341170b87d417dfb364133b825067aee734f2be9200f2ba073304cd614205dba7b434fc81e2e29e934177df33f047aba062b4cc8144379fb230264ba6c432efc240f65e932116cff3f4432b82c
[2/12/2014 07:52:58] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM settings WHERE param = 'fullscreen';?}
[2/12/2014 07:52:58] (conn: 1) Message not formatted correctly
81da6415b90a1f37d869107cd664462f9b7b1170cb7346399b7e05729b304661dc79103795281764d5285e37ea4f2850fa5e443f994c365af42a1770cd7e0d7bde794442f14f3650997a0567d8674428992d0260d5661776cb6f017b9e314668
[2/12/2014 07:52:59] (conn: 1)  Received Message: {"action":"query","tag":"test","sql":"SELECT * FROM settings WHERE param = 'fullscreen';F}
[2/12/2014 07:52:59] (conn: 1) Message not formatted correctly 

此问题已通过 npm update 命令修复 - 我的库安装是 5 天前的,3 天前进行了更新,似乎已经对其进行了排序。

相关内容

  • 没有找到相关文章

最新更新