Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
The MIT License Copyright (c) 2010 Tad Glines Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
 package com.glines.socketio.server.transport;
 
 import java.util.List;
 
 
 
 
 public class WebSocketTransport extends AbstractTransport implements WebSocketFactory.Acceptor {
 	public static final String TRANSPORT_NAME = "websocket";
 	public static final long CONNECTION_TIMEOUT = 10*1000;
 	private final WebSocketFactory wsFactory;
 	private final long maxIdleTime;
 
   @Override
   public WebSocket doWebSocketConnect(HttpServletRequest httpServletRequestString s) {
     throw new RuntimeException("This method isn't used by socket io");
   }
 
   @Override
   public String checkOrigin(HttpServletRequest httpServletRequestString hostString origin) {
     if (origin == null) {
       origin = host;
     }
     return origin;
   }
 
 		private final SocketIOSession session;
 		private Connection outbound = null;
 		private boolean initiated = false;
 
 			this. = session;
       session.setHeartbeat(/2);
       session.setTimeout();
 		}
 		
 		/*
 		 * (non-Javadoc)
 		 * @see org.eclipse.jetty.websocket.WebSocket#onConnect(org.eclipse.jetty.websocket.WebSocket.Outbound)
 		 */
 		public void onOpen(final Connection outbound) {
 			this. = outbound;
 		}
 
         /*
            * (non-Javadoc)
            * @see org.eclipse.jetty.websocket.WebSocket#onDisconnect()
            */
 		public void onClose(int codeString reason) {
 		}
 
 		/*
 		 * (non-Javadoc)
 		 * @see com.glines.socketio.SocketIOInbound.SocketIOOutbound#disconnect()
 		 */
 		public void disconnect() {
		}
		public void close() {
		}
		}
		public void sendMessage(SocketIOFrame framethrows SocketIOException {
			if (.isOpen()) {
				Log.debug("Session["+.getSessionId()+"]: sendMessage: [" + frame.getFrameType() + "]: " + frame.getData());
				try {
catch (IOException e) {
					throw new SocketIOException(e);
				}
else {
			}
		}
		/*
		 * (non-Javadoc)
		 * @see com.glines.socketio.SocketIOInbound.SocketIOOutbound#sendMessage(java.lang.String)
		 */
		public void sendMessage(String messagethrows SocketIOException {
		}
		public void sendMessage(int messageTypeString message)
				sendMessage(new SocketIOFrame(..messageTypemessage));
else {
			}
		}
		/*
		 * (non-Javadoc)
		 * @see com.glines.socketio.SocketIOSession.SessionTransportHandler#handle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, com.glines.socketio.SocketIOSession)
		 */
		public void handle(HttpServletRequest request,
				HttpServletResponse responseSocketIOSession sessionthrows IOException {
    		response.sendError(."Unexpected request on upgraded WebSocket connection");
    		return;
		}
		public void disconnectWhenEmpty() {
		}
		public void abort() {
			 = null;
		}
    @Override
    public void onMessage(String message) {
			if (!) {
				if ("OPEN".equals(message)) {
					try {
						 = true;
catch (IOException e) {
					}
else {
				}
else {
				List<SocketIOFramemessages = SocketIOFrame.parse(message);
				for (SocketIOFrame msgmessages) {
				}
			}
    }
  }
	public WebSocketTransport(int bufferSizeint maxIdleTime) {
		 = new WebSocketFactory(thisbufferSize);
		this. = maxIdleTime;
	}
	public String getName() {
	}
  protected String[] parseProtocols(String protocol)
  {
      if (protocol == null)
          return new String[]{null};
      protocol = protocol.trim();
      if (protocol == null || protocol.length() == 0)
          return new String[]{null};
      String[] passed = protocol.split("\\s*,\\s*");
      String[] protocols = new String[passed.length + 1];
      System.arraycopy(passed, 0, protocols, 0, passed.length);
      return protocols;
  }
	public void handle(HttpServletRequest request,
			Transport.InboundFactory inboundFactory,
			SocketIOSession.Factory sessionFactory)
			throws IOException {
		String sessionId = extractSessionId(request);
		if ("GET".equals(request.getMethod()) && sessionId == null && "websocket".equalsIgnoreCase(request.getHeader("Upgrade"))) {
			boolean hixie = request.getHeader("Sec-WebSocket-Protocol") != null;
      String protocol=request.getHeader(hixie ? "Sec-WebSocket-Protocol" : "WebSocket-Protocol");
      if (protocol == null)
          protocol=request.getHeader("Sec-WebSocket-Protocol");
      String host = request.getHeader("Host");
      String origin = checkOrigin(requesthostrequest.getHeader("Origin"));
      SocketIOInbound inbound = inboundFactory.getInbound(request);
      if (inbound == null) {
      } else {
        SessionWrapper wrapper = null;
        SocketIOSession session = sessionFactory.createSession(inbound);
        for (String p : parseProtocols(protocol))
        {
            wrapper = new SessionWrapper(session);
            if (wrapper != null)
            {
                protocol = p;
                break;
            }
        }
        .upgrade(request,response,wrapper,originprotocol);
      }
else {
    		response.sendError(."Invalid " +  + " transport request");
		}
	}
New to GrepCode? Check out our FAQ X