Error connection automatically closed 1006 by Chrome
LoisDuplain opened this issue · comments
Describe what you would like to know or do
Im trying to connect the my WS server (created using Java-WebSocket) with JavaScript.
But my connecgtion is automatically closed (exit code 1006)
Last version of Java-WebSocket.
Last version of Chrome browser.
App.java
package fr.ptut.etron;
import java.net.InetSocketAddress;
import org.java_websocket.server.WebSocketServer;
public class App {
private static final String HOST = "192.168.1.15";
private static final int PORT = 8887;
public static void main(String[] args) {
WebSocketServer server = new Serveur(new InetSocketAddress(HOST, PORT));
Thread commandThread = new Thread(new CommandThread(server));
commandThread.start();
server.setReuseAddr( true );
server.setTcpNoDelay( true );
server.run();
}
}
Serveur.java
package fr.ptut.etron;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.java_websocket.WebSocket;
import org.java_websocket.drafts.Draft;
import org.java_websocket.exceptions.InvalidDataException;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshakeBuilder;
public class Serveur extends WebSocketServer implements IConfig {
public Serveur(InetSocketAddress address) {
super(address);
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conn.send("Welcome to the server!"); //This method sends a message to the new client
broadcast( "new connection: " + handshake.getResourceDescriptor() ); //This method sends a message to all clients connected
System.out.println("new connection to " + conn.getRemoteSocketAddress());
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
System.out.println("closed " + conn.getRemoteSocketAddress() + " with exit code " + code + " additional info: " + reason);
}
@Override
public void onMessage(WebSocket conn, String message) {
System.out.println("received message from " + conn.getRemoteSocketAddress() + ": " + message);
}
@Override
public void onMessage( WebSocket conn, ByteBuffer message ) {
System.out.println("received ByteBuffer from " + conn.getRemoteSocketAddress());
}
@Override
public void onError(WebSocket conn, Exception ex) {
System.err.println("an error occurred on connection " + conn.getRemoteSocketAddress() + ":" + ex);
}
@Override
public void onStart() {
System.out.println("server started successfully");
}
}
<!DOCTYPE html>
<html>
<head>
<title>Etron</title>
<script>
const exampleSocket = new WebSocket("ws://86.***.***.*:8887/", "prot");
exampleSocket.onopen = (event) => {
exampleSocket.send("I opened the connection with the server");
};
exampleSocket.onmessage = (event) => {
console.log("Server: " + event.data);
}
exampleSocket.addEventListener('onclose', (event) => {
console.log('Event onclose: ', event);
});
exampleSocket.addEventListener('error', (event) => {
console.log('Event error: ', event);
});
function sendLeft() {
exampleSocket.send("gauche");
}
</script>
<style>
button {
width: 50%;
height: 100vh;
background-color: #fbc531;
border: none;
cursor: pointer;
}
button:hover {
background-color: #e1b12c;
}
.arrow {
color: white;
height: 5rem;
}
</style>
</head>
<body style="margin: 0; padding: 0;">
<div style="display: flex;">
<button id="buttonLeft" type="button" onclick="sendLeft()">
<svg class="arrow" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M10.5 19.5L3 12m0 0l7.5-7.5M3 12h18"></path>
</svg>
</button>
<button id="buttonRight" type="button">
<svg class="arrow" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" aria-hidden="true">
<path stroke-linecap="round" stroke-linejoin="round" d="M13.5 4.5L21 12m0 0l-7.5 7.5M21 12H3"></path>
</svg>
</button>
</div>
</body>
</html>
The server displays this error when i try to connet to using my index.html
thank you for the detailed report! I will look at this in the next few days, meanwhile can you try making a minimal websocket client with this library and try to connect from that?
code for a basic client can be found here: https://github.com/TooTallNate/Java-WebSocket/wiki#client-example
Sorry, I forgot to update the issue.
The issue is solved for my case but i don't know if it can cause issue to someone else.
To solved it I just removed the protocol parameter ("prot") line 6 in my index.html
When i wrote it i didn't see this parameter was optional and I don't really understand how it works.
What is sure is that depending on the version of chrome it could work.
Chrome Windows: did not work
Chrome iPhone: works
Mozilla Linux / Android: works
Safari iPhone / Mac: works
Thanks for the update
Closing this as solved