我通過web套接字發送文本到Java服務器,但從不調用onpen函數這是我使用的客戶端(WebSocketTest)函數,當我關閉服務器時,的的OnClose功能警報消息被正確地稱爲WebSocket的onpen函數不起作用
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:4444");
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!");
}
}
這是我在服務器
GET/HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:4444
Origin: null
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: 4BAiV8AU80juonjYQw5V9g==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36
這recieving是在服務器端
public class InvoiceListener {
private static BufferedReader in;
private final static int port = 4444;
private static ServerSocket listenSocket;
private static Socket client;
private static Invoice invoice;
private static String info;
public static void main(String[] args) throws IOException {
PrintInvoice printer;
ServerSocket listenSocket = new ServerSocket(port);
System.out.println("Listening");
client = listenSocket.accept();
System.out.println("client connected !");
in = new BufferedReader(new InputStreamReader(
client.getInputStream()));
while ((info = in.readLine()) != null) {
System.out.println(info);
}
}
}
您使用的是什麼Web服務器? – Shahe
java,使用java ServerSocket –
什麼是您正在測試的瀏覽器?和什麼版本? – Shahe