Announcement Announcement Module
Collapse
No announcement yet.
Using Tomcat 7.0.27 websocket functionality. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using Tomcat 7.0.27 websocket functionality.

    Hi all, i have been following websockets for sometime. Here are two of the java implementations i found.
    1.) jWebSocket - standalone, tomcat application, *.jar full-bundle
    2.) Tomcat 7.0.27.0 - org.apache.catalina.websocket.WebSocketServlet

    Below are a few ways i figured out, but unsure how can i go ahead.

    Q1.) jWebSocket has a websocket factory. How can i set the XML so that i load the class as a bean and start it when the application starts?

  • #2
    I did try load an external Servlet, but not sure if i did it correctly.

    I have a class MyWebSocket.java
    Code:
    package com.rts.web.rmdls;
    import java.io.IOException;
    import java.nio.ByteBuffer;
    import java.nio.CharBuffer;
    import java.util.List;
    import java.util.Set;
    import java.util.concurrent.CopyOnWriteArraySet;
    import java.util.concurrent.atomic.AtomicInteger;
    // From tomcat 7.0.27.0
    import org.apache.catalina.websocket.MessageInbound;
    import org.apache.catalina.websocket.StreamInbound;
    import org.apache.catalina.websocket.WebSocketServlet;
    import org.apache.catalina.websocket.WsOutbound;
    
    // Same example from tomcat website
    // WebSocketServlet is a servlet implementation
    public class MyWebSocket extends WebSocketServlet{
        ............. other code
    }
    web.xml
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>
        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>*.htm</url-pattern>
        </servlet-mapping>
    
        <!-- ADDED NEW SERVLET-->
        <servlet>
            <servlet-name>websocketTest</servlet-name>
            <servlet-class>com.testapp.web.MyWebSocket</servlet-class>
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
            <servlet-name>websocketTest</servlet-name>
            <url-pattern>*.do</url-pattern>
        </servlet-mapping>
        
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
        
        <welcome-file-list>
            <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

    How do i verify that my new servlet is loaded?
    I wrote a test page but http 404 returned for my websocket.

    Cheers!
    Many thanks!

    Comment


    • #3
      Sorry, it did work. The problem was the url i used for WebSocket

      Javascript code
      HTML Code:
      <script type="text/javascript">
      
          <!-- 
                  Conrrect url
                  ws://localhost:8080/myapp/test.do
          -->
          var soc = new WebSocket('ws://' + window.location.host +'/myapp/test.do
      
          <!-- 
                  Inconrrect url
                  ws://localhost:8080/test.do
          -->
          var soc = new WebSocket('ws://' + window.location.host +'/test.do
      
      
      
      </script>
      Cheers!

      Comment

      Working...
      X