Package org.jwebsocket.kit

Examples of org.jwebsocket.kit.WebSocketException


    public void open(String uriString) throws WebSocketException {
        URI uri = null;
        try {
            uri = new URI(uriString);
        } catch (URISyntaxException e) {
            throw new WebSocketException("Error parsing WebSocket URL:" + uriString, e);
        }
        this.url = uri;
        handshake = new WebSocketHandshake(url);
        try {
            socket = createSocket();
            input = socket.getInputStream();
            output = new PrintStream(socket.getOutputStream());

            output.write(handshake.getHandshake());

            boolean handshakeComplete = false;
            boolean header = true;
            int len = 1000;
            byte[] buffer = new byte[len];
            int pos = 0;
            ArrayList<String> handshakeLines = new ArrayList<String>();

            byte[] serverResponse = new byte[16];

            while (!handshakeComplete) {
                status = WebSocketStatus.CONNECTING;
                int b = input.read();
                buffer[pos] = (byte) b;
                pos += 1;

                if (!header) {
                    serverResponse[pos - 1] = (byte) b;
                    if (pos == 16) {
                        handshakeComplete = true;
                    }
                } else if (buffer[pos - 1] == 0x0A && buffer[pos - 2] == 0x0D) {
                    String line = new String(buffer, "UTF-8");
                    if (line.trim().equals("")) {
                        header = false;
                    } else {
                        handshakeLines.add(line.trim());
                    }

                    buffer = new byte[len];
                    pos = 0;
                }
            }

            handshake.verifyServerStatusLine(handshakeLines.get(0));
            handshake.verifyServerResponse(serverResponse);

            handshakeLines.remove(0);

            Map<String, String> headers = new FastMap<String, String>();
            for (String line : handshakeLines) {
                String[] keyValue = line.split(": ", 2);
                headers.put(keyValue[0], keyValue[1]);
            }
            handshake.verifyServerHandshakeHeaders(headers);

            receiver = new WebSocketReceiver(input);

            // TODO: Add event parameter
            // notifyOpened(null);

            receiver.start();
            connected = true;
            status = WebSocketStatus.OPEN;
        } catch (WebSocketException wse) {
            throw wse;
        } catch (IOException ioe) {
            throw new WebSocketException("error while connecting: " + ioe.getMessage(), ioe);
        }
    }
View Full Code Here


    }

    @Override
    public void send(byte[] data) throws WebSocketException {
        if (!connected) {
            throw new WebSocketException("error while sending binary data: not connected");
        }
        try {
            if (isBinaryData) {
                output.write(0x80);
                // TODO: what if frame is longer than 255 characters (8bit?) Refer to IETF spec!
                output.write(data.length);
                output.write(data);               
            } else {
                output.write(0x00);
                output.write(data);
                output.write(0xff);
            }
            output.flush();
        } catch (IOException ioe) {
            throw new WebSocketException("error while sending binary data: ", ioe);
        }
    }
View Full Code Here

        byte[] data;
        try {
            data = aData.getBytes(aEncoding);
            send(data);
        } catch (UnsupportedEncodingException e) {
            throw new WebSocketException("Encoding exception while sending the data:" + e.getMessage(), e);
        }
    }
View Full Code Here

            socket.shutdownInput();
            socket.shutdownOutput();
            socket.close();
            status = WebSocketStatus.CLOSED;
        } catch (IOException ioe) {
            throw new WebSocketException("error while closing websocket connection: ", ioe);
        }
        // TODO: add event
        notifyClosed(null);
    }
View Full Code Here

        notifyClosed(null);
    }

    private void sendCloseHandshake() throws WebSocketException {
        if (!connected) {
            throw new WebSocketException("error while sending close handshake: not connected");
        }
        try {
            output.write(0xff00);
            // TODO: check if final CR/LF is required/valid!
            output.write("\r\n".getBytes());
            // TODO: shouldn't we put a flush here?
        } catch (IOException ioe) {
            throw new WebSocketException("error while sending close handshake", ioe);
        }
        connected = false;
    }
View Full Code Here

                port = 80;
            }
            try {
                socket = new Socket(host, port);
            } catch (UnknownHostException uhe) {
                throw new WebSocketException("unknown host: " + host, uhe);
            } catch (IOException ioe) {
                throw new WebSocketException("error while creating socket to " + url, ioe);
            }
        } else if (scheme != null && scheme.equals("wss")) {
            if (port == -1) {
                port = 443;
            }
            try {
                SocketFactory factory = SSLSocketFactory.getDefault();
                socket = factory.createSocket(host, port);
            } catch (UnknownHostException uhe) {
                throw new WebSocketException("unknown host: " + host, uhe);
            } catch (IOException ioe) {
                throw new WebSocketException("error while creating secure socket to " + url, ioe);
            }
        } else {
            throw new WebSocketException("unsupported protocol: " + scheme);
        }

        return socket;
    }
View Full Code Here

      EngineListener listener = new EngineListener(this);
      mEngineThread = new Thread(listener);
      mEngineThread.start();

    } catch (IOException ex) {
      throw new WebSocketException(ex.getMessage());
    }

    // TODO: results in firing started event twice! make more clean!
    // super.startEngine();
    if (mLog.isInfoEnabled()) {
View Full Code Here

   *             if there's an exception while initialization
   */
  public final WebSocketInitializer initialize() throws WebSocketException {
    String lConfigPath = JWebSocketConfig.getConfigurationPath();
    if (lConfigPath == null) {
      throw new WebSocketException(
          "Either JWEBSOCKET_HOME variable is not set"
          + " or jWebSocket.xml file does neither exist at %JWEBSOCKET_HOME%/conf"
          + " nor at %CLASSPATH%/conf.");
    }
    // load the entire settings from the configuration xml file
View Full Code Here

      XMLStreamReader lStreamReader = null;
      lStreamReader = lFactory.createXMLStreamReader(lFIS);
      lConfig = mConfigHandler.processConfig(lStreamReader);
    } catch (XMLStreamException ex) {
      lMsg = ex.getClass().getSimpleName() + " occurred while creating XML stream (" + aConfigFilePath + ").";
      throw new WebSocketException(lMsg);
    } catch (FileNotFoundException ex) {
      lMsg = "jWebSocket config file not found while creating XML stream (" + aConfigFilePath + ").";
      throw new WebSocketException(lMsg);
    }
    return lConfig;
  }
View Full Code Here

TOP

Related Classes of org.jwebsocket.kit.WebSocketException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.