嗨,我有一个与我合作的树莓派,我想通过一个网页连接到它,为此,我有一个WebSocket连接设置,以便可以将HTML页面的输入发送到树莓派。这是我的javascript文件中的代码:
function sendData()
{
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://localhost:9998/echo");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
alert("Message is received...");
};
ws.onclose = function()
{
// websocket is closed.
alert("Connection is closed...");
};
}
else {
// The browser doesn't support WebSocket
alert("WebSocket NOT supported by your Browser!");
}
}
但是,当我运行该程序时,它将不会发送消息“要发送的消息”,并且我将收到WebSocketException:握手状态200或连接超时错误。我知道树莓派会连接到该网站,因为它会获取有关我的浏览器的信息。但是ws.open无法打开并发送消息。