我正在尝试使用http://www.tutorialspoint.com/html5/html5_websocket.htm中示例中的 HTML5 连接到 C# TCP 服务器
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function WebSocketTest()
{
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!");
}
}
</script>
</head>
<body>
<div id="sse">
<a href="javascript:WebSocketTest()">Run WebSocket</a>
</div>
</body>
</html>
TCP服务器运行在本地机器的4530端口上。所以我改变了
var ws = new WebSocket("ws://localhost:9998/echo");
至
var ws = new WebSocket("ws://localhost:4530");
当我运行该页面时,我收到消息WebSocket is supported by your Browser!它挂在那里。有什么帮助吗?