I am working on a simple server, just to learn how it works. I got a java server and java client to work, but trying to get a javascript client to connect to the same java server is driving me crazy!
Java server:
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 client: (which works)
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 client:
<!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>
When I run the java client with the server I get:
Starting
While
Accepted
Message from client: I'm the client
Sending message...
Closing socket
While
and output from the client:
Sending message...
Hi
So that works. When I run the javascript client, the server's output is:
Starting
While
Accepted
Message from client: GET / HTTP/1.1
Sending message...
Closing socket
While
And the output on the javascript page is:
Error: [object Event]
Disconnected: undefined
Also, firefox console says "Firefox can't establish a connection to the server at ws://localhost:9091/". What am I missing? Thanks!
A webSocket connection from a browser is not just a plain TCP connection. It MUST support the full webSocket protocol or the browser will not be able to connect to it or exchange data. This full webSocket protocol includes:
You can read more about that here: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.