Reputation: 13
I'm pretty new to websocket programming (or better: web programming at all). A while ago I made something using Sockets and ServerSockets to exchange data between a client and server and now I started to build something with similiar functionality using WebSockets and jetty but can't figure it out until now.
So what I'm aiming to do at the moment is: the client got a string message, sends it to the server using a websocket and the server is just pasting out that message (to console or to a file). General context: I got 2 applications running on different machines and want to use websockets to exchange some data between them.
So that's my client's code :
public class SocketClient {
public static void main(String[] args) throws URISyntaxException {
try {
new SocketClient().run(new URI("ws://localhost:8080/"));
//new SocketClient().run(new URI("wss://localhost:8443/"));
} catch (Exception e) {
e.printStackTrace();
}
}
public void run(URI destURI) throws Exception {
WebSocketClient client = new WebSocketClient();
try {
client.start();
ClientUpgradeRequest request = new ClientUpgradeRequest();
ExampleSocket socket = new ExampleSocket();
Future<Session> future = client.connect(socket,destURI,request);
future.get();
socket.getRemote().sendString("hello server");
}
finally
{
client.stop();
}
}
}
class ExampleSocket extends WebSocketAdapter
{
@Override
public void onWebSocketText(String message)
{
try
{
getRemote().sendString(message);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
And that's my server code:
public class SocketServer extends AbstractHandler {
public void handle(String target, Request baseRequest,HttpServletRequest request, HttpServletResponse response) {
try {
response.setContentType("text/html");
response.setStatus(HttpServletResponse.SC_OK);
baseRequest.setHandled(true);
response.getWriter().println(request);
}
catch (IOException ioe) {
System.out.println(ioe);
}
}
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
server.setHandler(new SocketServer());
server.start();
server.join();
}
}
I got both mostly from the jetty documentations but can't figure it out until now. When I try running them together I get that error on my client:
java.util.concurrent.ExecutionException: org.eclipse.jetty.websocket.api.UpgradeException: Didn't switch protocols
at org.eclipse.jetty.util.FuturePromise.get(FuturePromise.java:123)
at SocketClient.run(SocketClient.java:37)
at SocketClient.main(SocketClient.java:19)
Caused by: org.eclipse.jetty.websocket.api.UpgradeException: Didn't switch protocols
at org.eclipse.jetty.websocket.client.io.UpgradeConnection.validateResponse(UpgradeConnection.java:278)
at org.eclipse.jetty.websocket.client.io.UpgradeConnection.read(UpgradeConnection.java:205)
at org.eclipse.jetty.websocket.client.io.UpgradeConnection.onFillable(UpgradeConnection.java:150)
at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:540)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
at java.lang.Thread.run(Unknown Source)
So thanks a lot in advance, any help is appreciated.
Upvotes: 1
Views: 11112
Reputation: 11
There may be the server started in secure connection mode and trying to connect with non secure URL or vice versa. This can be the root cause of this exception.
Upvotes: 0
Reputation: 1131
WebSocket client needs WebSocket server. What you did in server is to add a handler to deal with HTTP request-response exchanges only. You need to allow Jetty to deal with WebSocket resources by registering a servlet of org.eclipse.jetty.websocket.servlet.WebSocketServlet
or an endpoint of javax.websocket.Endpoint
.
Here's working examples written by Jetty team. If you are not a fan of JSR, take a look at native-jetty-websocket-example.
Anyway, Jetty team will give you definite answer if this answer is incomplete as they are watching jetty tag.
Upvotes: 3