我最近能够在浏览器和服务器之间建立连接..但是过去两天连接没有建立......我无法弄清楚出了什么问题?
<head>
<script type="text/javascript">
function WebSocketTest()
{
if ("WebSocket" in window)
{
var x=document.getElementById('chat').value;
// Let us open a web socket
var ws = new WebSocket("ws://localhost:7869/echo");
ws.onopen = function()
{
// Web Socket is connected, send data using send()
ws.send(x);
var send=document.getElementById('fill');
send.innerHTML = send.innerHTML+'<br>'+ x;
};
ws.onmessage = function (evt)
{
var received_msg = evt.data;
var send=document.getElementById('fill');
send.innerHTML = send.innerHTML +'<br>'+ received_msg;
};
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">
<input type="text" id="chat"></div><br>
<button type='submit' onclick='javascript:WebSocketTest()'>Send</button>
<div id='fill'>hi</div>
</body>
这是代码和websocket服务器正在监听7869端口。