1
我正在一個簡單的服務器上工作,只是爲了瞭解它是如何工作的。我有一個Java服務器和Java客戶端工作,但試圖讓一個JavaScript客戶端連接到同一個Java服務器正在讓我瘋狂!Javascript客戶端無法連接到java服務器
Java服務器:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class DateServer {
public static void main(String[] args) throws IOException {
ServerSocket listener = new ServerSocket(9091);
System.out.println("Starting");
try {
while (true) {
System.out.println("While");
Socket socket = listener.accept();
System.out.println("Accepted");
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String msg = input.readLine();
System.out.println("Message from client: " + msg);
try {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
System.out.println("Sending message...");
out.println("Hi");
} finally {
System.out.println("Closing socket");
socket.close();
}
}
}
finally {
listener.close();
}
}
}
Java客戶端:(工作)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.JOptionPane;
public class DateClient {
public static void main(String[] args) throws IOException {
Socket s = new Socket("localhost", 9091);
try {
PrintWriter out =
new PrintWriter(s.getOutputStream(), true);
System.out.println("Sending message...");
out.println("I'm the client");
} finally {
//System.out.println("Closing socket");
//s.close();
}
BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream()));
String msg = input.readLine();
System.out.println(msg);
JOptionPane.showMessageDialog(null, msg);
s.close();
System.exit(0);
}
}
JavaScript客戶端:
<!DOCTYPE html>
<html>
<head>
<title>Echo Test</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<div id="messages"></div>
<script type="text/javascript">
var connection = new WebSocket('ws://localhost:9091/');
var messages = document.getElementById("messages");
// When the connection is open, send some data to the server
connection.onopen = function (e) {
writeResponse('sent');
connection.send('Ping');
}
// Log errors
connection.onerror = function (error) {
writeResponse('Error: ' + error);
}
connection.onclose = function(e){
writeResponse("Disconnected: " + e.data);
}
// Log messages from the server
connection.onmessage = function (e) {
writeResponse('Server: ' + e.data);
}
function writeResponse(text){
messages.innerHTML += "<br/>" + text;
}
</script>
</body>
</html>
當我運行我得到服務器的Java客戶端:
Starting
While
Accepted
Message from client: I'm the client
Sending message...
Closing socket
While
從客戶端
輸出:
Sending message...
Hi
使作品。當我運行JavaScript客戶端,服務器的輸出是:
Starting
While
Accepted
Message from client: GET/HTTP/1.1
Sending message...
Closing socket
While
和JavaScript頁面上的輸出:
Error: [object Event]
Disconnected: undefined
此外,火狐控制檯說:「火狐無法建立與連接服務器在ws:// localhost:9091 /「。我錯過了什麼?謝謝!
謝謝!該頁面指向我寫入HTTP響應的正確方向併發送正確的sec-websocket-accept密鑰,並且客戶端現在連接到服務器:) – Arlo
@Arlo - 僅供參考,現在已有類實現webSocket服務器Java的。您可能只想抓住其中一個,而不是自己重新實施整個協議。 – jfriend00