javascript - WebSocket application doesn't work properly when deployed on Tomcat8 server -
the problem
i'm trying run example minimal changes in source code: https://spring.io/guides/gs/messaging-stomp-websocket/
i have managed run using eclipse , maven, terminal window, again, maven. i've managed package .war file , run in terminal using java -jar myfile.war
works expected in cases, i.e. localhost:8080 displays simple interface , connects , returns greeting in same window. problem comes when deploy .war file tomcat8 server have running on raspberrypi. in case, index.html displays, "connect" button nothing, leads me believe sort of javascript related issue. server works fine, btw, have other stuff deployed on it.
things i've tried
- edited .war archive index.html , .js files outside web-inf directory, may accessible outside world. problem persists.
- changed script references .js files in index.html to
<script src="http://cdn.sockjs.org/sockjs-0.3.4.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/stomp.js/2.3.3/stomp.js"></script>
so may delivered on cdn, in case there problem .war file. problem persists.
- added functionality servlet displays data in different directory make sure works , not issue .war file. seems fine, above problem persists.
code
entry point
@springbootapplication public class application extends springbootservletinitializer { @override protected springapplicationbuilder configure(springapplicationbuilder application) { return application.sources(application.class); } public static void main(string[] args) { springapplication.run(application.class, args); } }
greeting returned.
public class greeting { private string content; public greeting(string content) { this.content = content; } public string getcontent() { return content; } }
controller
@controller public class greetingcontroller { @messagemapping("/hello") @sendto("/topic/greetings") public greeting greeting(hellomessage message) throws exception { thread.sleep(3000); // simulated delay return new greeting("hello, " + message.getname() + "!"); } }
message sent
public class hellomessage { private string name; public string getname() { return name; } }
websocket configuration
@configuration @enablewebsocketmessagebroker public class websocketconfig extends abstractwebsocketmessagebrokerconfigurer { @override public void configuremessagebroker(messagebrokerregistry config) { config.enablesimplebroker("/topic"); config.setapplicationdestinationprefixes("/app"); } public void registerstompendpoints(stompendpointregistry registry) { registry.addendpoint("/hello").withsockjs(); } }
index.html
<!doctype html> <html> <head> <title>hello websocket</title> <script src="sockjs-0.3.4.js"></script> <script src="stomp.js"></script> <script> var stompclient = null; function setconnected(connected) { document.getelementbyid('connect').disabled = connected; document.getelementbyid('disconnect').disabled = !connected; document.getelementbyid('conversationdiv').style.visibility = connected ? 'visible' : 'hidden'; document.getelementbyid('response').innerhtml = ''; } function connect() { var socket = new sockjs('/hello'); stompclient = stomp.over(socket); stompclient.connect({}, function(frame) { setconnected(true); console.log('connected: ' + frame); stompclient.subscribe('/topic/greetings', function(greeting){ showgreeting(json.parse(greeting.body).content); }); }); } function disconnect() { if (stompclient != null) { stompclient.disconnect(); } setconnected(false); console.log("disconnected"); } function sendname() { var name = document.getelementbyid('name').value; stompclient.send("/app/hello", {}, json.stringify({ 'name': name })); } function showgreeting(message) { var response = document.getelementbyid('response'); var p = document.createelement('p'); p.style.wordwrap = 'break-word'; p.appendchild(document.createtextnode(message)); response.appendchild(p); } </script> </head> <body onload="disconnect()"> <noscript><h2 style="color: #ff0000">seems browser doesn't support javascript! websocket relies on javascript being enabled. please enable javascript , reload page!</h2></noscript> <div> <div> <button id="connect" onclick="connect();">connect</button> <button id="disconnect" disabled="disabled" onclick="disconnect();">disconnect</button> </div> <div id="conversationdiv"> <label>what name?</label><input type="text" id="name" /> <button id="sendname" onclick="sendname();">send</button> <p id="response"></p> </div> </div> </body> </html>
pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>org.springframework</groupid> <artifactid>gs-messaging-stomp-websocket</artifactid> <version>0.1.0</version> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.3.3.release</version> </parent> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-websocket</artifactid> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-messaging</artifactid> </dependency> </dependencies> <properties> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> <packaging>war</packaging> </project>
log last deployment
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: spring boot :: (v1.3.3.release) 2016-04-21 22:39:51.727 info 18022 --- [o-8080-exec-129] hello.application : starting application on raspberrypi pid 18022 (/var/lib/tomcat8/webapps/gs-messaging-stomp-websocket-0.1.0/web-inf/classes/hello/application.class started tomcat8 in /var/lib/tomcat8) 2016-04-21 22:39:51.800 info 18022 --- [o-8080-exec-129] hello.application : no active profile set, falling default profiles: default 2016-04-21 22:39:52.752 info 18022 --- [o-8080-exec-129] ationconfigembeddedwebapplicationcontext : refreshing org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@167fc4: startup date [thu apr 21 22:39:52 eest 2016]; root of context hierarchy 2016-04-21 22:40:09.630 info 18022 --- [o-8080-exec-129] o.s.b.f.s.defaultlistablebeanfactory : overriding bean definition bean 'beannameviewresolver' different definition: replacing [root bean: class [null]; scope=; abstract=false; lazyinit=false; autowiremode=3; dependencycheck=0; autowirecandidate=true; primary=false; factorybeanname=org.springframework.boot.autoconfigure.web.errormvcautoconfiguration$whitelabelerrorviewconfiguration; factorymethodname=beannameviewresolver; initmethodname=null; destroymethodname=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/errormvcautoconfiguration$whitelabelerrorviewconfiguration.class]] [root bean: class [null]; scope=; abstract=false; lazyinit=false; autowiremode=3; dependencycheck=0; autowirecandidate=true; primary=false; factorybeanname=org.springframework.boot.autoconfigure.web.webmvcautoconfiguration$webmvcautoconfigurationadapter; factorymethodname=beannameviewresolver; initmethodname=null; destroymethodname=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/webmvcautoconfiguration$webmvcautoconfigurationadapter.class]] 2016-04-21 22:40:16.208 info 18022 --- [o-8080-exec-129] o.s.web.context.contextloader : root webapplicationcontext: initialization completed in 23468 ms 2016-04-21 22:40:30.525 info 18022 --- [o-8080-exec-129] b.a.w.tomcatwebsocketcontainercustomizer : nonembeddedservletcontainerfactory detected. websockets support should native not problem. 2016-04-21 22:40:40.343 info 18022 --- [o-8080-exec-129] o.s.b.c.e.servletregistrationbean : mapping servlet: 'dispatcherservlet' [/] 2016-04-21 22:40:40.360 info 18022 --- [o-8080-exec-129] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'errorpagefilter' to: [/*] 2016-04-21 22:40:40.363 info 18022 --- [o-8080-exec-129] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'characterencodingfilter' to: [/*] 2016-04-21 22:40:40.366 info 18022 --- [o-8080-exec-129] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'hiddenhttpmethodfilter' to: [/*] 2016-04-21 22:40:40.369 info 18022 --- [o-8080-exec-129] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'httpputformcontentfilter' to: [/*] 2016-04-21 22:40:40.372 info 18022 --- [o-8080-exec-129] o.s.b.c.embedded.filterregistrationbean : mapping filter: 'requestcontextfilter' to: [/*] 2016-04-21 22:40:41.904 info 18022 --- [o-8080-exec-129] o.s.s.concurrent.threadpooltaskexecutor : initializing executorservice 'clientinboundchannelexecutor' 2016-04-21 22:40:42.137 info 18022 --- [o-8080-exec-129] o.s.s.concurrent.threadpooltaskexecutor : initializing executorservice 'clientoutboundchannelexecutor' 2016-04-21 22:40:42.743 info 18022 --- [o-8080-exec-129] o.s.s.c.threadpooltaskscheduler : initializing executorservice 'messagebrokertaskscheduler' 2016-04-21 22:40:44.228 info 18022 --- [o-8080-exec-129] o.s.w.s.s.s.websockethandlermapping : mapped url path [/hello/**] onto handler of type [class org.springframework.web.socket.sockjs.support.sockjshttprequesthandler] 2016-04-21 22:40:44.635 info 18022 --- [o-8080-exec-129] o.s.s.concurrent.threadpooltaskexecutor : initializing executorservice 'brokerchannelexecutor' 2016-04-21 22:40:49.712 info 18022 --- [o-8080-exec-129] .websocketannotationmethodmessagehandler : mapped "{[/hello],messagetype=[message]}" onto public hello.greeting hello.greetingcontroller.greeting(hello.hellomessage) throws java.lang.exception 2016-04-21 22:40:57.059 info 18022 --- [o-8080-exec-129] s.w.s.m.m.a.requestmappinghandleradapter : looking @controlleradvice: org.springframework.boot.context.embedded.annotationconfigembeddedwebapplicationcontext@167fc4: startup date [thu apr 21 22:39:52 eest 2016]; root of context hierarchy 2016-04-21 22:40:58.966 info 18022 --- [o-8080-exec-129] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.modelandview org.springframework.boot.autoconfigure.web.basicerrorcontroller.errorhtml(javax.servlet.http.httpservletrequest,javax.servlet.http.httpservletresponse) 2016-04-21 22:40:58.993 info 18022 --- [o-8080-exec-129] s.w.s.m.m.a.requestmappinghandlermapping : mapped "{[/error]}" onto public org.springframework.http.responseentity<java.util.map<java.lang.string, java.lang.object>> org.springframework.boot.autoconfigure.web.basicerrorcontroller.error(javax.servlet.http.httpservletrequest) 2016-04-21 22:40:59.092 info 18022 --- [o-8080-exec-129] o.s.w.s.c.a.webmvcconfigureradapter : adding welcome page: servletcontext resource [/index.html] 2016-04-21 22:40:59.622 info 18022 --- [o-8080-exec-129] o.s.w.s.handler.simpleurlhandlermapping : root mapping handler of type [class org.springframework.web.servlet.mvc.parameterizableviewcontroller] 2016-04-21 22:40:59.948 info 18022 --- [o-8080-exec-129] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2016-04-21 22:40:59.950 info 18022 --- [o-8080-exec-129] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2016-04-21 22:41:01.167 info 18022 --- [o-8080-exec-129] o.s.w.s.handler.simpleurlhandlermapping : mapped url path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.resourcehttprequesthandler] 2016-04-21 22:41:06.289 info 18022 --- [o-8080-exec-129] o.s.j.e.a.annotationmbeanexporter : registering beans jmx exposure on startup 2016-04-21 22:41:06.561 info 18022 --- [o-8080-exec-129] o.s.c.support.defaultlifecycleprocessor : starting beans in phase 2147483647 2016-04-21 22:41:06.570 info 18022 --- [o-8080-exec-129] o.s.m.s.b.simplebrokermessagehandler : starting... 2016-04-21 22:41:06.578 info 18022 --- [o-8080-exec-129] o.s.m.s.b.simplebrokermessagehandler : brokeravailabilityevent[available=true, simplebrokermessagehandler [defaultsubscriptionregistry[cache[0 destination(s)], registry[0 sessions]]]] 2016-04-21 22:41:06.597 info 18022 --- [o-8080-exec-129] o.s.m.s.b.simplebrokermessagehandler : started. 2016-04-21 22:41:06.830 info 18022 --- [o-8080-exec-129] hello.application : started application in 93.812 seconds (jvm running 2602144.954) apr 21, 2016 10:41:07 pm org.apache.catalina.startup.hostconfig deploywar info: deployment of web application archive /var/lib/tomcat8/webapps/gs-messaging-stomp-websocket-0.1.0.war has finished in 137,294 ms 2016-04-21 22:41:31.098 info 18022 --- [o-8080-exec-131] o.s.web.servlet.dispatcherservlet : frameworkservlet 'dispatcherservlet': initialization started 2016-04-21 22:41:31.455 info 18022 --- [o-8080-exec-131] o.s.web.servlet.dispatcherservlet : frameworkservlet 'dispatcherservlet': initialization completed in 355 ms 2016-04-21 22:41:44.554 info 18022 --- [messagebroker-1] o.s.w.s.c.websocketmessagebrokerstats : websocketsession[0 current ws(0)-httpstream(0)-httppoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompsubprotocol[processed connect(0)-connected(0)-disconnect(0)], stompbrokerrelay[null], inboundchannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundchannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockjsscheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0] 2016-04-21 23:11:44.549 info 18022 --- [messagebroker-1] o.s.w.s.c.websocketmessagebrokerstats : websocketsession[0 current ws(0)-httpstream(0)-httppoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompsubprotocol[processed connect(0)-connected(0)-disconnect(0)], stompbrokerrelay[null], inboundchannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundchannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockjsscheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 1] 2016-04-21 23:41:44.549 info 18022 --- [messagebroker-1] o.s.w.s.c.websocketmessagebrokerstats : websocketsession[0 current ws(0)-httpstream(0)-httppoll(0), 0 total, 0 closed abnormally (0 connect failure, 0 send limit, 0 transport error)], stompsubprotocol[processed connect(0)-connected(0)-disconnect(0)], stompbrokerrelay[null], inboundchannel[pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], outboundchannelpool size = 0, active threads = 0, queued tasks = 0, completed tasks = 0], sockjsscheduler[pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 2]
link .war file
http://s000.tinyupload.com/index.php?file_id=67742984079178858914
final thoughts
again, process of elimination, either .js libraries aren't importing properly, or there wrong server's websocket support. i'm stuck on one, , appreciated.
so may happening once deploy service paths little off, lets have link says <a href="/home" > home </a>
work on local not work on server because application url host:8080/appname/ , when refer /home instead of taking host:8080/appname/home take host:8080/home, @ point broken url. think can fix issue changing /hello in here
function connect() { var socket = new sockjs('hello'); stompclient = stomp.over(socket); stompclient.connect({}, function(frame) { setconnected(true); console.log('connected: ' + frame); stompclient.subscribe('/topic/greetings', function(greeting){ showgreeting(json.parse(greeting.body).content); }); }); }
Comments
Post a Comment