I am trying to run my WebSockets Application on my Raspberry PI. I've downloaded and unpacked Tomcat 7.0.67. Then I started the Tomcat-Manager and deployed my "wsock.jar" that only contains one file:
// ChatServer.java
package wsock;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
@ServerEndpoint("/chat")
public class ChatServer {
@OnOpen
public void onOpen(Session session) {
System.err.println("Opened session: " + session.getId());
}
@OnClose
public void onClose(Session session) {
System.err.println("Closed session: " + session.getId());
}
@OnMessage
public String onMessage(String message) {
System.err.println("Message received: " + message);
return "{ \"message\": \"Hello World\" }";
}
}
When deploying on my local Tomcat 7 (currently on Windows 10) it works:
ws = new WebSocket('ws://localhost:8080/wsock/chat');
WebSocket { url: "ws://localhost:8080/wsock/chat", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, onclose: null, extensions: "", protocol: "", onmessage: null, binaryType: "blob" }
When deploying on my Raspberry PI:
ws = new WebSocket('ws://raspberrypi:8080/wsock/chat');
WebSocket { url: "ws://raspberrypi:8080/wsock/chat", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, onclose: null, extensions: "", protocol: "", onmessage: null, binaryType: "blob" }
Firefox can't establish a connection to the server at ws://raspberrypi:8080/wsock/chat.
The request that was send is:
Host: raspberrypi:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
DNT: 1
Sec-WebSocket-Version: 13
Origin: http://raspberrypi:8080
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: 0a80/+qiZ3mJ03bDgSV5kg==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
But the answer doesn't contain the upgrade:
Content-Language: en
Content-Length: 971
Content-Type: text/html;charset=utf-8
Date: Fri, 01 Jan 2016 11:42:35 GMT
Server: Apache-Coyote/1.1
The interesting thing is, that when I connect to the examples WebSocket Chat (shipped with the Tomcat server) it works:
ws = new WebSocket('ws://raspberrypi:8080/examples/websocket/chat');
WebSocket { url: "ws://raspberrypi:8080/examples/webs…", readyState: 0, bufferedAmount: 0, onopen: null, onerror: null, onclose: null, extensions: "", protocol: "", onmessage: null, binaryType: "blob" }
So, what's wrong here? What am I missing? What can I do to resolve this problem?
I found the answer after hours reading config files and testing on different machines. It was the Java version. My Raspberry PI runs Raspbian Linux which is based on Debian. On both my Debian and Raspberry machine it didn't work. On my Windows and Ubuntu machine, which both have Java 8 it worked. Debian and Raspbian have Java 7. My project was build with Java 8 support which lead to this problem.
I changed the Java version in my Eclipse project and now it works.