消息上iisnode WebSocket,接收的数据是二进制消息而不是字符串

irlmq6kh  于 2023-10-20  发布在  其他
关注(0)|答案(1)|浏览(145)

我已经使用ws模块创建了一个iisnode WebSocket服务器。我遇到的问题是,虽然客户端发送字符串数据到服务器,服务器接收的数据作为一个二进制消息。
客户端JS

var socket; 
window.addEventListener('load', function(){ 
    socket = new WebSocket('wss://server/ws/');
    
    socket.addEventListener('open', function (event) {
        console.log('Hello Server!');
        socket.send('Hello Server!');
    });
    
    socket.addEventListener('message', function (event) {
        console.log('Message from server : ', event.data);
    });
    
    socket.onerror = function(event){
        console.log('Error : ', event);
    }; 
    
    socket.onclose = function(event){
        console.log('Close : ', event);
    }; 
});

服务器端JS

var express     = require("express"); 
var app         = express(); 
var WebSocket   = require("ws"); 

var server = app.listen(process.env.PORT, function(){
    console.log("listening");
});

var wss = new WebSocket.Server({"server":server}); 
wss.on("connection", function(user){ 
    user.send("connected"); 
    user.on("message", function(message){ 
        wss.clients.forEach(function(client){ 
            if(client.readyState===WebSocket.OPEN){ 
                client.send(typeof(message)); 
                client.send(message); 
            }; 
        }); 
    }); 
}); 

app.get("/ws/", function(req, res) {
    res.send("WebSocket");
});

web.cofig

<?xml version="1.0" encoding="UTF-8"?>
<configuration> 
    <system.webServer>
        <handlers>
            <add name="iisnode" path="node_app.js" verb="*" modules="iisnode" />
        </handlers>
        <rewrite>
            <rules>
                <rule name="ws" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^ws/?$" />
                    <action type="Rewrite" url="\ws\node_app.js" />
                </rule>
            </rules>
        </rewrite>
        <webSocket enabled="false" />
    </system.webServer> 
</configuration>

当我运行代码时,我得到的数据是:

| ↑↓       | Data           | Value                                                            |
| -------- | -------------- | ---------------------------------------------------------------- |
| ↑        | Hello Server!  | Hello Server!                                                    |
| ↓        | connected      | connected                                                        |
| ↓        | object         | object                                                           |
| ↓        | Binary Message | 00000000: 4865 6c6c 6f20 5365 7276 6572 21         Hello Server! |

从我在网上看到的一切,我在服务器端接收的数据应该是一个字符串“你好服务器!“所以我不确定为什么数据服务器端是一个对象?

cgyqldqp

cgyqldqp1#

所以在我读过的所有东西中,没有人提到这一点,但message.toString()似乎已经做到了这一点。
服务器端JS

var express     = require("express"); 
var app         = express(); 
var WebSocket   = require("ws"); 

var server = app.listen(process.env.PORT, function(){
    console.log("listening");
});

var wss = new WebSocket.Server({"server":server}); 
wss.on("connection", function(user){ 
    user.send("connected"); 
    user.on("message", function(message){ 
        var message = message.toString(); 
        wss.clients.forEach(function(client){ 
            if(client.readyState===WebSocket.OPEN){ 
                client.send(typeof(message)); 
                client.send(message); 
            }; 
        }); 
        message = null; 
    }); 
}); 

app.get("/ws/", function(req, res) {
    res.send("WebSocket");
});

相关问题