向客户端发送套接字消息时出错

时间:2020-07-29 09:16:33

标签: javascript c# vue.js sockets

当我尝试将数据从套接字服务器(C#)发送到套接字客户端(Javascript)时,出现以下错误:

与'ws://127.0.0.1/'的WebSocket连接失败:一个或多个保留位打开:reserved1 = 1,reserved2 = 1,reserved3 = 1

我的C#部分看起来像这样:

...
// ip and port are correct, and the connection can be established
_serverSocket = new TcpListener(ip, port);
_serverSocket.Start();
_client = await _serverSocket.AcceptSocketAsync();

// Accept some Messages from Client

if(//handle the ^GET request)
{
   ...
}
else
{
    // read Data from client => Works, and I can really extract the string im getting sent
    
    // HERE I WANT TO SEND SOME DATA BACK:
    var data = new MyClass() { sucess: true, msg: ""}; // This class should not be important
    _client.SendAsync(new ArraySegment<byte> 
       (Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data))), SocketFlags.None);
} 

我在Vue.js中的javascript部分:

data() {
    return {
        ws: null,
        sendData: "SomeData",
        status: "",
        initDone: false
    };
},
computed: {
    connectionEstablished() {
        return this.ws != null;
    }
},
methods: {
    start() {
        try {
            this.ws = new WebSocket("ws://127.0.0.1:80");

            this.ws.addEventListener('message', function (event) {
                console.log('Message from server ', event.data);
            });

            this.ws.onopen = event => {
                this.status = "Connection established";
                this.initDone = true;
            };
            this.ws.onclose = event => {
                if (this.initDone) {
                    if (event.code == 1000) {
                        this.status = "Connection closed";
                    } else {
                        this.status = "Connection lost";
                    }
                } else {
                    this.status = "Connection could not be established";
                }
                this.ws = null;
                this.initDone = false;
            };
            this.ws.onerror = event => {
                console.log('onerror', event);
                this.status = "Connection error";
            };
        } catch (error) {
            this.ws = null;
            this.status = "Connection error: " + error;
        }
    },
    stop() {
        if (this.ws != null) {
            if (this.ws.readyState === 1) this.ws.close();
            this.ws = null;
            this.status = "Connection closed";
        }
    },
    send() {
        let send = JSON.stringify({
            type: "program",
            call: this.sendData
        });

        let that = this;
        this.waitForConnection(function() {
            that.ws.send(send);
        }, 1000);
    },
    waitForConnection(callback, interval) {
        if (this.ws.readyState === 1) {
            callback();
        } else {
            var that = this;
            console.log(this.ws);
            // optional: implement backoff for interval here
            setTimeout(function() {
                that.waitForConnection(callback, interval);
            }, interval);
        }
    },
    dataReceived(event) {
        this.status = "Data received: " + event.data;
    },
    initialize() {}
}

我希望我的代码不要太短。如果您需要任何更具体的实现,我将尝试为您添加它们。

我的第一个想法是,如果需要在C#部分中发送某种Header,但是互联网上的所有教程都只发送带有消息的字符串,就是这样。

感谢您的帮助!

0 个答案:

没有答案