Example usage for com.google.gwt.corp.websocket WebSocket create

List of usage examples for com.google.gwt.corp.websocket WebSocket create

Introduction

In this page you can find the example usage for com.google.gwt.corp.websocket WebSocket create.

Prototype

public static WebSocket create(String url, String string) throws IOException 

Source Link

Usage

From source file:com.googlecode.gwtquake.shared.client.WebSocketFactoryImpl.java

License:Open Source License

public void send(NetworkAddress adr, byte[] buf, int len) throws IOException {
    // TODO(haustein): check if addess still matches?

    if (socket == null) {
        remotePort = adr.port;//  w ww . j  a  v a  2s. c  o  m
        remoteIp = new byte[4];
        System.arraycopy(adr.ip, 0, remoteIp, 0, 4);

        String url = "ws://" + InetAddress.getByAddress(adr.ip).getHostAddress() + ":" + adr.port;
        System.out.println("connect for send to: " + url);

        socket = WebSocket.create(url, "" + localPort);

        System.out.println("socket: " + socket);

        socket.setListener(new WebSocket.Listener() {
            public void onOpen(WebSocket socket, OpenEvent event) {
                connected = true;
            }

            public void onMessage(WebSocket socket, MessageEvent event) {
                String data = event.getData();
                msgQueue.add(data);
            }

            public void onClose(WebSocket socket, CloseEvent event) {
                connected = false;
            }
        });
    }

    //      System.out.println("sending: " + connected+ " " + Lib.hexDump(buf, len, false));
    if (connected) {
        socket.send(Compatibility.bytesToString(buf, len));
    }
}

From source file:jake2.client.WebSocketFactoryImpl.java

License:Open Source License

public void send(netadr_t adr, byte[] buf, int len) throws IOException {
    // TODO(haustein): check if addess still matches?

    if (socket == null) {
        remotePort = adr.port;//from w ww  .j  a  v  a2s.  c  o m
        remoteIp = new byte[4];
        System.arraycopy(adr.ip, 0, remoteIp, 0, 4);

        String url = "ws://" + InetAddress.getByAddress(adr.ip).getHostAddress() + ":" + adr.port;
        System.out.println("connect for send to: " + url);

        socket = WebSocket.create(url, "" + localPort);

        System.out.println("socket: " + socket);

        socket.setListener(new WebSocket.Listener() {
            public void onOpen(WebSocket socket, OpenEvent event) {
                connected = true;
            }

            public void onMessage(WebSocket socket, MessageEvent event) {
                String data = event.getData();
                msgQueue.add(data);
            }

            public void onClose(WebSocket socket, CloseEvent event) {
                connected = false;
            }
        });
    }

    //      System.out.println("sending: " + connected+ " " + Lib.hexDump(buf, len, false));
    if (connected) {
        socket.send(Compatibility.bytesToString(buf, len));
    }
}