我試圖連接到使用HTML5從例如C#TCP服務器http://www.tutorialspoint.com/html5/html5_websocket.htmHTML5 WebSocket連接到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由您的瀏覽器支持!它掛在那裏。任何幫助?
你的服務器是否支持正確的'WebSocket'協議?它可能取決於您使用的[瀏覽器](http://en.wikipedia.org/wiki/WebSocket#Browser_support) –
您的意思是tcp服務器應該也支持Websocket?就瀏覽器而言,是不是消息*** WebSocket是否被瀏覽器支持!***足夠了? – Mika
Websockets在TCP之上使用特殊的協議,包括一些額外的連接握手。這不像正常的套接字,你可以連接並開始通信。 –