webSocket.send()没有在react js中执行?



我在reactjs中使用WebSocket,但是WebSocket。send没有在onClick事件上执行。这里我创建WebSocket

const ws = new WebSocket("ws://192.168.18.112:8000/ws/notification/");

连接WebSocket

ws.onopen = (e) => {
console.log("connect");
};
这是我的onClick函数
const sendNotification = (e) => {
console.log("click");
if (ws.readyState === WebSocket.OPEN && message !== "") {
ws.send("message");
console.log("Sending message"); // this console is execute
} else if (ws.readyState === WebSocket.CONNECTING) {
ws.addEventListener("open", () => sendNotification());
console.log("Connecting state");
console.log("Not Send");
} else {
console.log("nothing happen");
}
};

所以这是我的问题,我无法在我的代码中找到错误或问题。

try thisW3CWebSocket。我也不认为有什么"/"after notification afterws/notification仔细检查你的终点是否正确。

mport React, { Component } from 'react';
import { w3cwebsocket as W3CWebSocket } from "websocket";
const client = new W3CWebSocket('ws://192.168.18.112:8000/ws/notification/');
class App extends Component {
componentWillMount() {
client.onopen = () => {
console.log('WebSocket Client Connected');
};
client.onmessage = (message) => {
console.log(message);
};
}

render() {
return (
<div>
Practical Intro To WebSockets.
</div>
);
}
}
export default App;

最新更新