You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by J Java <jf...@gmail.com> on 2014/02/18 12:42:01 UTC

Websocket on tomcat 7.0.50 not working

I am trying to implement a websocket on tomcat 7.0.50 using annotated
endpoints as specified in Java Websocket API (1.0)- JSR 356. Following are
the brief steps how I have coded it 1) Write a websocket endpoint using
@ServerEndpoint annotation 2) implement @onOpen and @onMessage methods 3)
open a websocket using javascript on google chrome.

Please find code corresponding to above steps in order

1) STEP 1 & 2 - writing websocket server endpoint:

        package com.jkweb.websocket;

       import java.io.IOException;
       import java.util.HashMap;
       import java.util.List;
       import java.util.Map;

       import javax.websocket.EndpointConfig;
       import javax.websocket.OnMessage;
       import javax.websocket.OnOpen;
       import javax.websocket.Session;
       import javax.websocket.server.PathParam;
       import javax.websocket.server.ServerEndpoint;

       import org.slf4j.Logger;
       import org.slf4j.LoggerFactory;

  @ServerEndpoint(value="/websocket/fileuploadtracker")
@OnOpen public void open(Session session,EndpointConfig config) {
    ......}@OnMessagepublic void onMessage(Session session, String msg) {
    try {
        session.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }}
public static void sendMessage(String uniqueTocken,String msg){
    try {
        Session wsSession = socketConnectionMap.get(uniqueTocken);
        wsSession.getBasicRemote().sendText(msg);
    } catch (IOException e) {
        logger.error(e.getMessage());
    }}

}

2) STEP 3 - opening websocket using javascript api in chrome:

      wsurl =  "ws://localhost:8080/jkweb/websocket/fileuploadtracker",
        ws;
        ws = new WebSocket(wsurl);
         ws.onopen = function()
         {
            // Web Socket is connected, send data using send()
            ws.send("Sending first Message");
            alert("Message is sent...");
         };
         ws.onmessage = function (evt)
         {
            var received_msg = evt.data;
            alert("Message is received...");
         };
         ws.onclose = function(evt)
         {
            // websocket is closed.
            alert("Connection is closed..."+evt.code + ":"+evt.reason );
         };

I am using following versions of softwares : 1) Tomcat - 7.0.50 2) Java -
1.7.45 3) Servlet - 3.0 4) Have included following Maven dependency 5)
Chrome - 32.0.1700.107m

     <dependency>
     <groupId>javax.websocket</groupId>
     <artifactId>javax.websocket-api</artifactId>
     <version>1.0</version>
         <scope>provided</scope></dependency>

However I am getting following error and connection is closed with 1066
error code in chrome's console:

      WebSocket connection to
'ws://localhost:8080/jkweb/websocket/fileuploadtracker' failed: Error
during WebSocket handshake: Unexpected response code: 404

Is there any configuration I am missing out on. I tried to search a lot but
couldn't find anything. Please I need this to be solved ASAP.
Thanks,
Shailesh

Re: Websocket on tomcat 7.0.50 not working

Posted by J Java <jf...@gmail.com>.
Hey guys,

 I have solved it. Problem was very specific to my installation.
I had the websocket api .jar being installed in my app's WEB-INF/lib
directory as well.
Not sure why it was breaking it. can you put some light on the root cause
of this behaviour and why it doesn't work when you gave websocket-api.jar
in application's WEB-INF/lib along with tomcat's lib?
If possible you can answer the similar question I have posted on
stackoverflow.com as well.

Thanks,
Shailesh


On Tue, Feb 18, 2014 at 8:06 PM, J Java <jf...@gmail.com> wrote:

> Another Update:
>
>  I downloaded 8.0.3 as well and started it without eclipse and deployed
> the app but still the same result. How to check if tomcat is creating the
> server endpoints for my websocket?
>
> I don't understand what I am doing wrong?
>
> Guys,
> need your help ASAP.
>
> Thanks,
> Shailesh.
>
>
> On Tue, Feb 18, 2014 at 7:41 PM, J Java <jf...@gmail.com> wrote:
>
>> yes Nikki. its jkweb
>>
>> Thanks,
>> Shailesh
>>
>>
>>
>> On Tue, Feb 18, 2014 at 6:53 PM, Niki Dokovski <ni...@gmail.com> wrote:
>>
>>> On Tue, Feb 18, 2014 at 3:10 PM, J Java <jf...@gmail.com> wrote:
>>>
>>> > just an update :
>>> >
>>> > this is what i get in tomcat access logs :
>>> >
>>> > 127.0.0.1 - - [18/Feb/2014:16:42:45 +0530] "GET
>>> > /jkweb/websocket/fileuploadtracker HTTP/1.1" 404 1017
>>> >
>>> > is this correct ??
>>> >
>>>
>>> Can you check the context root of the deployed application? Is it
>>> "jkweb"?
>>>
>>> Niki
>>>
>>>
>>> >
>>> > On Tue, Feb 18, 2014 at 5:56 PM, J Java <jf...@gmail.com>
>>> wrote:
>>> >
>>> > > Also I using Struts 2. but my Struts2 servlet only handles urls with
>>> > > pattern : *.action
>>> > > <filter-mapping>
>>> > > <filter-name>struts2</filter-name>
>>> > >  <url-pattern>*.action</url-pattern>
>>> > > </filter-mapping>
>>> > >
>>> > > Also I have configured Spring security but logs show that it is
>>> filtering
>>> > > the request properly.
>>> > >
>>> > >
>>> > > On Tue, Feb 18, 2014 at 5:42 PM, J Java <jf...@gmail.com>
>>> wrote:
>>> > >
>>> > >> I have currently deployed it through Eclipse Juno. However I am
>>> *not *
>>> > using
>>> > >> Eclipse's instance. My webapp gets deployed in my tomcat and not
>>> > Eclipse's
>>> > >> one.
>>> > >> Please let me know any additional information you need. Or please
>>> let me
>>> > >> know how to go debugging it like checking if Websocket Server
>>> endpoints
>>> > are
>>> > >> actully mounted to accept the connections or not and other
>>> diagnostics?
>>> > >>
>>> > >> Thanks,
>>> > >> Shailesh.
>>> > >>
>>> > >
>>> > >
>>> >
>>>
>>
>>
>

Re: Websocket on tomcat 7.0.50 not working

Posted by J Java <jf...@gmail.com>.
Another Update:

 I downloaded 8.0.3 as well and started it without eclipse and deployed the
app but still the same result. How to check if tomcat is creating the
server endpoints for my websocket?

I don't understand what I am doing wrong?

Guys,
need your help ASAP.

Thanks,
Shailesh.

On Tue, Feb 18, 2014 at 7:41 PM, J Java <jf...@gmail.com> wrote:

> yes Nikki. its jkweb
>
> Thanks,
> Shailesh
>
>
>
> On Tue, Feb 18, 2014 at 6:53 PM, Niki Dokovski <ni...@gmail.com> wrote:
>
>> On Tue, Feb 18, 2014 at 3:10 PM, J Java <jf...@gmail.com> wrote:
>>
>> > just an update :
>> >
>> > this is what i get in tomcat access logs :
>> >
>> > 127.0.0.1 - - [18/Feb/2014:16:42:45 +0530] "GET
>> > /jkweb/websocket/fileuploadtracker HTTP/1.1" 404 1017
>> >
>> > is this correct ??
>> >
>>
>> Can you check the context root of the deployed application? Is it "jkweb"?
>>
>> Niki
>>
>>
>> >
>> > On Tue, Feb 18, 2014 at 5:56 PM, J Java <jf...@gmail.com> wrote:
>> >
>> > > Also I using Struts 2. but my Struts2 servlet only handles urls with
>> > > pattern : *.action
>> > > <filter-mapping>
>> > > <filter-name>struts2</filter-name>
>> > >  <url-pattern>*.action</url-pattern>
>> > > </filter-mapping>
>> > >
>> > > Also I have configured Spring security but logs show that it is
>> filtering
>> > > the request properly.
>> > >
>> > >
>> > > On Tue, Feb 18, 2014 at 5:42 PM, J Java <jf...@gmail.com>
>> wrote:
>> > >
>> > >> I have currently deployed it through Eclipse Juno. However I am *not
>> *
>> > using
>> > >> Eclipse's instance. My webapp gets deployed in my tomcat and not
>> > Eclipse's
>> > >> one.
>> > >> Please let me know any additional information you need. Or please
>> let me
>> > >> know how to go debugging it like checking if Websocket Server
>> endpoints
>> > are
>> > >> actully mounted to accept the connections or not and other
>> diagnostics?
>> > >>
>> > >> Thanks,
>> > >> Shailesh.
>> > >>
>> > >
>> > >
>> >
>>
>
>

Re: Websocket on tomcat 7.0.50 not working

Posted by J Java <jf...@gmail.com>.
yes Nikki. its jkweb

Thanks,
Shailesh



On Tue, Feb 18, 2014 at 6:53 PM, Niki Dokovski <ni...@gmail.com> wrote:

> On Tue, Feb 18, 2014 at 3:10 PM, J Java <jf...@gmail.com> wrote:
>
> > just an update :
> >
> > this is what i get in tomcat access logs :
> >
> > 127.0.0.1 - - [18/Feb/2014:16:42:45 +0530] "GET
> > /jkweb/websocket/fileuploadtracker HTTP/1.1" 404 1017
> >
> > is this correct ??
> >
>
> Can you check the context root of the deployed application? Is it "jkweb"?
>
> Niki
>
>
> >
> > On Tue, Feb 18, 2014 at 5:56 PM, J Java <jf...@gmail.com> wrote:
> >
> > > Also I using Struts 2. but my Struts2 servlet only handles urls with
> > > pattern : *.action
> > > <filter-mapping>
> > > <filter-name>struts2</filter-name>
> > >  <url-pattern>*.action</url-pattern>
> > > </filter-mapping>
> > >
> > > Also I have configured Spring security but logs show that it is
> filtering
> > > the request properly.
> > >
> > >
> > > On Tue, Feb 18, 2014 at 5:42 PM, J Java <jf...@gmail.com>
> wrote:
> > >
> > >> I have currently deployed it through Eclipse Juno. However I am *not *
> > using
> > >> Eclipse's instance. My webapp gets deployed in my tomcat and not
> > Eclipse's
> > >> one.
> > >> Please let me know any additional information you need. Or please let
> me
> > >> know how to go debugging it like checking if Websocket Server
> endpoints
> > are
> > >> actully mounted to accept the connections or not and other
> diagnostics?
> > >>
> > >> Thanks,
> > >> Shailesh.
> > >>
> > >
> > >
> >
>

Re: Websocket on tomcat 7.0.50 not working

Posted by Niki Dokovski <ni...@gmail.com>.
On Tue, Feb 18, 2014 at 3:10 PM, J Java <jf...@gmail.com> wrote:

> just an update :
>
> this is what i get in tomcat access logs :
>
> 127.0.0.1 - - [18/Feb/2014:16:42:45 +0530] "GET
> /jkweb/websocket/fileuploadtracker HTTP/1.1" 404 1017
>
> is this correct ??
>

Can you check the context root of the deployed application? Is it "jkweb"?

Niki


>
> On Tue, Feb 18, 2014 at 5:56 PM, J Java <jf...@gmail.com> wrote:
>
> > Also I using Struts 2. but my Struts2 servlet only handles urls with
> > pattern : *.action
> > <filter-mapping>
> > <filter-name>struts2</filter-name>
> >  <url-pattern>*.action</url-pattern>
> > </filter-mapping>
> >
> > Also I have configured Spring security but logs show that it is filtering
> > the request properly.
> >
> >
> > On Tue, Feb 18, 2014 at 5:42 PM, J Java <jf...@gmail.com> wrote:
> >
> >> I have currently deployed it through Eclipse Juno. However I am *not *
> using
> >> Eclipse's instance. My webapp gets deployed in my tomcat and not
> Eclipse's
> >> one.
> >> Please let me know any additional information you need. Or please let me
> >> know how to go debugging it like checking if Websocket Server endpoints
> are
> >> actully mounted to accept the connections or not and other diagnostics?
> >>
> >> Thanks,
> >> Shailesh.
> >>
> >
> >
>

Re: Websocket on tomcat 7.0.50 not working

Posted by J Java <jf...@gmail.com>.
just an update :

this is what i get in tomcat access logs :

127.0.0.1 - - [18/Feb/2014:16:42:45 +0530] "GET
/jkweb/websocket/fileuploadtracker HTTP/1.1" 404 1017

is this correct ??


On Tue, Feb 18, 2014 at 5:56 PM, J Java <jf...@gmail.com> wrote:

> Also I using Struts 2. but my Struts2 servlet only handles urls with
> pattern : *.action
> <filter-mapping>
> <filter-name>struts2</filter-name>
>  <url-pattern>*.action</url-pattern>
> </filter-mapping>
>
> Also I have configured Spring security but logs show that it is filtering
> the request properly.
>
>
> On Tue, Feb 18, 2014 at 5:42 PM, J Java <jf...@gmail.com> wrote:
>
>> I have currently deployed it through Eclipse Juno. However I am *not * using
>> Eclipse's instance. My webapp gets deployed in my tomcat and not Eclipse's
>> one.
>> Please let me know any additional information you need. Or please let me
>> know how to go debugging it like checking if Websocket Server endpoints are
>> actully mounted to accept the connections or not and other diagnostics?
>>
>> Thanks,
>> Shailesh.
>>
>
>

Re: Websocket on tomcat 7.0.50 not working

Posted by J Java <jf...@gmail.com>.
Also I using Struts 2. but my Struts2 servlet only handles urls with
pattern : *.action
<filter-mapping>
<filter-name>struts2</filter-name>
 <url-pattern>*.action</url-pattern>
</filter-mapping>

Also I have configured Spring security but logs show that it is filtering
the request properly.


On Tue, Feb 18, 2014 at 5:42 PM, J Java <jf...@gmail.com> wrote:

> I have currently deployed it through Eclipse Juno. However I am *not * using
> Eclipse's instance. My webapp gets deployed in my tomcat and not Eclipse's
> one.
> Please let me know any additional information you need. Or please let me
> know how to go debugging it like checking if Websocket Server endpoints are
> actully mounted to accept the connections or not and other diagnostics?
>
> Thanks,
> Shailesh.
>

Re: Websocket on tomcat 7.0.50 not working

Posted by J Java <jf...@gmail.com>.
I have currently deployed it through Eclipse Juno. However I am *not * using
Eclipse's instance. My webapp gets deployed in my tomcat and not Eclipse's
one.
Please let me know any additional information you need. Or please let me
know how to go debugging it like checking if Websocket Server endpoints are
actully mounted to accept the connections or not and other diagnostics?

Thanks,
Shailesh.

Re: Websocket on tomcat 7.0.50 not working

Posted by Mark Thomas <ma...@apache.org>.
On 18/02/2014 11:42, J Java wrote:
> I am trying to implement a websocket on tomcat 7.0.50 using annotated
> endpoints as specified in Java Websocket API (1.0)- JSR 356. Following are
> the brief steps how I have coded it 1) Write a websocket endpoint using
> @ServerEndpoint annotation 2) implement @onOpen and @onMessage methods 3)
> open a websocket using javascript on google chrome.

I don't see anything immediately wrong.

<snip/>

> However I am getting following error and connection is closed with 1066
> error code in chrome's console:
> 
>       WebSocket connection to
> 'ws://localhost:8080/jkweb/websocket/fileuploadtracker' failed: Error
> during WebSocket handshake: Unexpected response code: 404

How have you deployed your web application?

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org