【发布时间】:2020-04-16 23:07:28
【问题描述】:
我正在尝试使用 express 连接到 websocket,但它没有连接。我正在尝试使用 WebSocket 向 HTML 页面发送消息。
我的服务器端
var expressWs = require('express-ws')(app);
app.ws('/echo', function (ws, req) {
ws.on('message', function (msg) {
ws.send(msg);
});
});
HTML 代码
<script language="javascript" type="text/javascript">
var form;
var websocket;
function init() {
form = document.getElementById("isyeribul");
websocket = new WebSocket("ws://localhost:8080/");
websocket.onopen = function(evt) {
console.log("CONNECTED");
};
websocket.onmessage = function(event) {
form.style.display = "none";
};
}
window.addEventListener("load", init, false);
function sendfunc(){
var text=document.getElementById("txt1").value;
websocket.send(text);
console.log(text+ " sent");
}
</script>
控制台输出
isyeriara:30 WebSocket is already in CLOSING or CLOSED state.
【问题讨论】:
标签: javascript html node.js express websocket