java - Jetty: socket connection as websocket connection -
i'm pretty new websocket programming (or better: web programming @ all). while ago made using sockets , serversockets exchange data between client , server , started build similiar functionality using websockets , jetty can't figure out until now.
so i'm aiming @ moment is: client got string message, sends server using websocket , server pasting out message (to console or file). general context: got 2 applications running on different machines , want use websockets exchange data between them.
so that's 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"); } { client.stop(); } } } class examplesocket extends websocketadapter { @override public void onwebsockettext(string message) { try { getremote().sendstring(message); } catch (ioexception e) { e.printstacktrace(); } } }
and that's 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 jetty documentations can't figure out until now. when try running them error on client:
java.util.concurrent.executionexception: org.eclipse.jetty.websocket.api.upgradeexception: didn't switch protocols @ org.eclipse.jetty.util.futurepromise.get(futurepromise.java:123) @ socketclient.run(socketclient.java:37) @ socketclient.main(socketclient.java:19) caused by: org.eclipse.jetty.websocket.api.upgradeexception: didn't switch protocols @ org.eclipse.jetty.websocket.client.io.upgradeconnection.validateresponse(upgradeconnection.java:278) @ org.eclipse.jetty.websocket.client.io.upgradeconnection.read(upgradeconnection.java:205) @ org.eclipse.jetty.websocket.client.io.upgradeconnection.onfillable(upgradeconnection.java:150) @ org.eclipse.jetty.io.abstractconnection$2.run(abstractconnection.java:540) @ org.eclipse.jetty.util.thread.queuedthreadpool.runjob(queuedthreadpool.java:635) @ org.eclipse.jetty.util.thread.queuedthreadpool$3.run(queuedthreadpool.java:555) @ java.lang.thread.run(unknown source)
so lot in advance, appreciated.
websocket client needs websocket server. did in server add handler deal http request-response exchanges only. need allow jetty deal websocket resources registering servlet of org.eclipse.jetty.websocket.servlet.websocketservlet
or endpoint of javax.websocket.endpoint
.
here's working examples written jetty team. if not fan of jsr, take @ native-jetty-websocket-example.
anyway, jetty team give definite answer if answer incomplete watching jetty tag.
Comments
Post a Comment