Jump to: navigation, search

Difference between revisions of "Jetty WTP Plugin/Jetty WTP Websocket Wizard"

(New page: {{ Jetty }} == Jetty Websocket Wizard == The Plug-In org.eclipse.jst.server.jetty.ui.websocket provides wizard to create Jetty WebSocket : * create an implementataion of the org.eclips...)
 
Line 16: Line 16:
  
  
[[Image:Jetty-wtp-websocket1.jpg]]
+
[[Image:Jetty-wtp-websocket2.jpg]]
  
 
Click on Next button, the wieard page displayed is the same than Servlet. Here you can define teh servlet-mapping :   
 
Click on Next button, the wieard page displayed is the same than Servlet. Here you can define teh servlet-mapping :   
  
  
[[Image:Jetty-wtp-websocket1.jpg]]
+
[[Image:Jetty-wtp-websocket3.jpg]]
  
  
Line 27: Line 27:
  
  
[[Image:Jetty-wtp-websocket1.jpg]]
+
[[Image:Jetty-wtp-websocket4.jpg]]
  
 
Click on Finish button, your workspace looks like this :
 
Click on Finish button, your workspace looks like this :
  
[[Image:Jetty-wtp-websocket1.jpg]]
+
[[Image:Jetty-wtp-websocket5.jpg]]
  
 
The wizard generate :
 
The wizard generate :

Revision as of 12:15, 18 November 2010



Jetty Websocket Wizard

The Plug-In org.eclipse.jst.server.jetty.ui.websocket provides wizard to create Jetty WebSocket :

  • create an implementataion of the org.eclipse.jetty.websocket.WebSocketServlet. which use an implementation of org.eclipse.jetty.websocket.WebSocket
  • modify the web.xml to declare the WebSocketServlet createdt-name + servlet-mapping).
  • TODO : create an HTML page wich call the WebSocketServlet.

To create a WebSocketServlet go at menu File/New/Other... and select Web/Jetty WebSocket :

Jetty-wtp-websocket1.jpg

The first page you fill the name of the Jetty WebSocket Servlet class (ex : ChatWebSocketServlet) and package (ex : org.sample.websocket).Supperclass field is automaticly filled with org.eclipse.jetty.websocket.WebSocketServlet  :


Jetty-wtp-websocket2.jpg

Click on Next button, the wieard page displayed is the same than Servlet. Here you can define teh servlet-mapping :


Jetty-wtp-websocket3.jpg


Click on Next button, here you can generate some methods. The doPost will be generated to use WebSocket using :


Jetty-wtp-websocket4.jpg

Click on Finish button, your workspace looks like this :

Jetty-wtp-websocket5.jpg

The wizard generate :

  • a class ChatWebSocketServlet :
package org.sample.websocket;
 
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketServlet;
 
/**
 * Jetty WebSocketServlet implementation class ChatWebSocketServlet
 */
public class ChatWebSocketServlet extends WebSocketServlet {
	private static final long serialVersionUID = 1L;
 
    /**
     * @see WebSocketServlet#WebSocketServlet()
     */
    public ChatWebSocketServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	private final Set<ChatWebSocket> members = new CopyOnWriteArraySet<ChatWebSocket>();
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		getServletContext().getNamedDispatcher("default").forward(request, response);
	}
 
	/*
	 * @see org.eclipse.jetty.websocket.WebSocketServlet#doWebSocketConnect(javax.servlet.http.HttpServletRequest, java.lang.String)
	 */
	protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol) {
		return new ChatWebSocket();
	}
 
	class ChatWebSocket implements WebSocket {
		private Outbound outbound;
 
		public void onConnect(Outbound outbound) {
			this.outbound = outbound;
			members.add(this);
		}
 
		/*
		 * @see org.eclipse.jetty.websocket.WebSocket#onMessage(byte, byte[],
		 * int, int)
		 */
		public void onMessage(byte frame, byte[] data, int offset, int length) {
		}
 
		/*
		 * @see org.eclipse.jetty.websocket.WebSocket#onMessage(byte,
		 * java.lang.String)
		 */
		public void onMessage(byte frame, String data) {
 
			for (ChatWebSocket member : members) {
				try {
					member.outbound.sendMessage(frame, data);
				} catch (IOException e) {
					// org.eclipse.jetty.util.log.Log.warn(e);
					e.printStackTrace();
				}
			}
		}
 
		/*
		 * @see org.eclipse.jetty.websocket.WebSocket#onDisconnect()
		 */
		public void onDisconnect() {
			members.remove(this);
		}
	}
 
 
}

1.web.xml is modified like this :

  <servlet>
    <description></description>
    <display-name>ChatWebSocketServlet</display-name>
    <servlet-name>ChatWebSocketServlet</servlet-name>
    <servlet-class>org.sample.websocket.ChatWebSocketServlet</servletclass>
  </servlet>
  <servlet-mapping>
    <servlet-name>ChatWebSocketServlet</servlet-name>
    <url-pattern>/ChatWebSocketServlet</url-pattern>
  </servlet-mapping>

2.TODO : generate an HTML file which consume the WebSocket.