WebSocket连接在发送消息时关闭

时间:2018-06-29 07:03:56

标签: javascript react-native websocket

所以,这就是我在本机反应中所做的事情。套接字已完美连接,可以接收消息但无法发送消息。

    constructor(props) {

    super(props);

    this.state = { messages: [] };
    // this.onSend = this.onSend.bind(this);

    this.socket = new WebSocket('ws://34.212.65.102/ws/chat/81b55636-c495-4270-ad91-21a7ec7e7c73/');

    this.socket.onopen = () => {
        console.log('Socket connected...!');
    };

    this.socket.onmessage = (e) => {
        console.log('A message was received',e.data);
    };

    this.socket.onerror = (e) => {
        // an error occurred
        console.log('An error occurred', e.message);
    };

    this.socket.onclose = (e) => {
        // 
        console.log('connection closed', e.code, e.reason);
    };
}

并发送消息代码

onSend(messages = []) {

    console.log('sending...');

    //this.socket.send(JSON.stringify({ "chat_uuid": "81b55636-c495-4270-ad91-21a7ec7e7c73", "message": "From vs code!!!!" }))
    let message = {
        'chat_uuid': '81b55636-c495-4270-ad91-21a7ec7e7c73',
        'message': {"chat_uuid":"81b55636-c495-4270-ad91-21a7ec7e7c73","message":"testtesfdfdfdfdfdfdfdttest"}
    }

    this.socket.send(JSON.stringify(message));
}

这是一个错误:

connection closed 1011 null

同样的东西在chrome扩展 Simple WebSocket Client

上也能正常工作

这是我要附加的扩展程序截图: enter image description here

不知道我在做什么错。

1 个答案:

答案 0 :(得分:0)

您的代码发送的消息与Chrome扩展程序发送的消息不同。也许您应该尝试只发送

this.socket.send(JSON.stringify({"chat_uuid":"81b55636-c495-4270-ad91-21a7ec7e7c73","message":"testtesfdfdfdfdfdfdfdttest"}));