[英]Javascript WebSocket server not receiving messages
我制作了一个非常简单的WebSocket应用程序以学习基础知识,虽然取得了一些成功,但是遇到了一个似乎无法解决的问题,主要是因为我没有遇到任何错误。
客户:
const url = 'ws://localhost:1720';
const ws = new WebSocket(url);
$("#sendbtn").click(function() {
console.log("clicked");
var input = $('#messageinput').val();
ws.send(input);
});
服务器:
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 1720 });
wss.on('connection', function(ws) {
console.log("New connection");
});
wss.onmessage = function(event) {
console.log("WebSocket message received:", event);
};
当我打开浏览器窗口时,服务器成功记录了“新连接”,但是当我尝试通过#sendbtn按钮将消息发送到服务器时,服务器中没有任何反应,它不会记录该消息。
我直接从文件API运行它,即不在XAMPP上运行,这可能是这里的问题吗? 感谢您的任何建议!
您如何尝试文档中显示的示例。
wss.on('connection', function connection(ws) {
ws.on('message', function incoming(message) {
console.log('received: %s', message);
});
ws.send('something');
});
事件是代码中的ws对象
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.