Struggling with JS websocket



我想发送以下websocket:

Index.html:

<script>
websocket = new WebSocket('wss://websocketurl', 'xmpp');
websocket.send('<message to="somevalue" type="somevalue" xmlns="somevalue"><body>Foo</body><nick xmlns="http://someurl">Foo1</nick></message>')
</script>

但当我转到index.html时,我会得到以下

结果

此外,似乎没有出现转义报价问题

提前感谢

从您共享的代码来看,您是否正在连接到服务器或设置事件以侦听响应?

你可以试试这样的东西-

<html>
<head>
<script>
// const websocket = new WebSocket('wss://websocketurl', 'xmpp');
// websocket.send(`<message to="somevalue" type="somevalue" xmlns="somevalue"><body>Foo</body><nick xmlns="http://someurl">Foo1</nick></message>`)
function WebSocket() {
if ("WebSocket" in window) {
alert("WebSocket is supported by your Browser!");
var ws = new WebSocket('wss://websocketurl', 'xmpp');//This would be the route to your server
ws.onopen = () => { //once the connection is open you can send a message
ws.send("Message to send");
alert("Message is sent...");
};
ws.onmessage = (e) => { //listens for the event from the server
var _msg = e.data;
alert("Message is received...");
}
ws.onclose = () => {
alert("Connection is closed...");
};
}
else {
alert("WebSocket Not Supported by your browser!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocket()">Run WebSocket</a>
</div>
</body>
</html>

让我知道这是否有帮助,或者我是否在这里偏离了基地。

我发现这是LiveHTTPServerVS代码扩展的问题,它显示的HTML和javascript不正确。我用另一台服务器打开了页面,然后它在上工作

最新更新