See More

package javaforce.service; import java.io.*; import java.net.*; import javaforce.*; /** WebSocket * * @author pquiring * * See : RFC 6455 */ public class WebSocket { protected InputStream is; protected OutputStream os; protected String host; protected String url; protected String[] cookies; private boolean connected = true; private String server_host; private String client_host; public static final int TYPE_CONT = 0x0; //do not use public static final int TYPE_TEXT = 0x1; public static final int TYPE_BINARY = 0x2; protected static final int TYPE_CLOSE = 0x08; protected static final int TYPE_PING = 0x09; protected static final int TYPE_PONG = 0x0a; public static final int FIN = 0x80; public static final int MASK = 0x80; public WebSocket(String server_host, String client_host, InputStream is, OutputStream os, String host, String url, String[] cookies) { if (server_host.equals("0:0:0:0:0:0:0:1")) { server_host = "127.0.0.1"; } this.server_host = server_host; if (client_host.equals("0:0:0:0:0:0:0:1")) { client_host = "127.0.0.1"; } this.client_host = client_host; this.is = is; this.os = os; this.host = host; this.url = url; this.cookies = cookies; } /** Free to use data */ public Object userobj; /** Returns URL used during WebSocket connection request. */ public String getURL() { return url; } public String getClientHost() { return client_host; } public String getServerHost() { return server_host; } public String getHost() { return host; } public InputStream getInputStream() { return is; } public OutputStream getOutputStream() { return os; } public String[] getCookies() { return cookies; } public String getCookie(String name) { name += "="; for(int a=0;a 16777216) { throw new Exception("WebSocket message > 16MB"); } if (len > 65535) { //create 64bit packet length byte[] header = new byte[10]; header[0] = (byte)(FIN | type); header[1] = 127; //bytes 2-6 : not supported (64bit length), only support 24bit header[7] = (byte)((len & 0xff0000) >> 16); header[8] = (byte)((len & 0xff00) >> 8); header[9] = (byte)(len & 0xff); os.write(header); } else if (len >= 126) { //create 16bit packet length byte[] header = new byte[4]; header[0] = (byte)(FIN | type); header[1] = 126; header[2] = (byte)((len & 0xff00) >> 8); header[3] = (byte)(len & 0xff); os.write(header); } else { //create 7bit packet length byte[] header = new byte[2]; header[0] = (byte)(FIN | type); header[1] = (byte)len; os.write(header); } os.write(msg); } catch (SocketException e) { connected = false; JFLog.logTrace("WebSocket closed:" + client_host); return false; } catch (Exception e) { connected = false; JFLog.log(e); return false; } return true; } public boolean isConnected() { return connected; } }