added readyState support

This commit is contained in:
Animesh Kumar 2011-02-12 17:33:37 +05:30 committed by Fil Maj
parent 28f27e89e4
commit 98d51b18e2
2 changed files with 54 additions and 8 deletions

View File

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2010 Animesh Kumar (https://github.com/anismiles) * Copyright (c) 2010 Animesh Kumar (https://github.com/anismiles)
* Copyright (c) 2010 Strumsoft (https://strumsoft.com)
* *
* Permission is hereby granted, free of charge, to any person * Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation * obtaining a copy of this software and associated documentation
@ -69,4 +70,9 @@
WebSocket.prototype.close = function() { WebSocket.prototype.close = function() {
this.socket.close(); this.socket.close();
} }
WebSocket.prototype.getReadyState = function() {
this.socket.getReadyState();
}
})(); })();

View File

@ -64,7 +64,24 @@ public class WebSocket implements Runnable {
DRAFT75, DRAFT76 DRAFT75, DRAFT76
} }
////////////////// CONSTANT // //////////////// CONSTANT
/**
* The connection has not yet been established.
*/
public final static int WEBSOCKET_STATE_CONNECTING = 0;
/**
* The WebSocket connection is established and communication is possible.
*/
public final static int WEBSOCKET_STATE_OPEN = 1;
/**
* The connection is going through the closing handshake.
*/
public final static int WEBSOCKET_STATE_CLOSING = 2;
/**
* The connection has been closed or could not be opened.
*/
public final static int WEBSOCKET_STATE_CLOSED = 3;
/** /**
* An empty string * An empty string
*/ */
@ -110,7 +127,7 @@ public class WebSocket implements Runnable {
*/ */
public static final byte DATA_END_OF_FRAME = (byte) 0xFF; public static final byte DATA_END_OF_FRAME = (byte) 0xFF;
////////////////// INSTANCE Variables // //////////////// INSTANCE Variables
/** /**
* The WebView instance from Phonegap DroidGap * The WebView instance from Phonegap DroidGap
*/ */
@ -183,6 +200,10 @@ public class WebSocket implements Runnable {
* Key3 used in handshake * Key3 used in handshake
*/ */
private byte[] key3 = null; private byte[] key3 = null;
/**
* The readyState attribute represents the state of the connection.
*/
private int readyState = WEBSOCKET_STATE_CONNECTING;
/** /**
* Constructor. * Constructor.
@ -243,6 +264,8 @@ public class WebSocket implements Runnable {
* Closes connection with server * Closes connection with server
*/ */
public void close() { public void close() {
this.readyState = WebSocket.WEBSOCKET_STATE_CLOSING;
// close socket channel // close socket channel
try { try {
this.socketChannel.close(); this.socketChannel.close();
@ -254,6 +277,8 @@ public class WebSocket implements Runnable {
// fire onClose method // fire onClose method
this.onClose(); this.onClose();
this.readyState = WebSocket.WEBSOCKET_STATE_CLOSED;
} }
/** /**
@ -264,7 +289,11 @@ public class WebSocket implements Runnable {
*/ */
public void send(String text) { public void send(String text) {
try { try {
if(this.readyState == WEBSOCKET_STATE_OPEN){
_send(text); _send(text);
} else {
this.onError(new NotYetConnectedException());
}
} catch (IOException e) { } catch (IOException e) {
this.onError(e); this.onError(e);
} }
@ -298,10 +327,20 @@ public class WebSocket implements Runnable {
} }
/** /**
* Builds text for javascript engine to invoke proper event method with proper data. * @return the readyState
*/
public int getReadyState() {
return readyState;
}
/**
* Builds text for javascript engine to invoke proper event method with
* proper data.
* *
* @param event websocket event (onOpen, onMessage etc.) * @param event
* @param msg Text message received from websocket server * websocket event (onOpen, onMessage etc.)
* @param msg
* Text message received from websocket server
* @return * @return
*/ */
private String buildJavaScriptData(String event, String msg) { private String buildJavaScriptData(String event, String msg) {
@ -350,7 +389,7 @@ public class WebSocket implements Runnable {
// actual connection logic // actual connection logic
private void _connect() throws IOException { private void _connect() throws IOException {
this.readyState = WEBSOCKET_STATE_CONNECTING;
socketChannel = SocketChannel.open(); socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false); socketChannel.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(uri.getHost(), port)); socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
@ -564,6 +603,7 @@ public class WebSocket implements Runnable {
} }
if (isConnectionReady) { if (isConnectionReady) {
this.readyState = WEBSOCKET_STATE_OPEN;
// fire onOpen method // fire onOpen method
this.onOpen(); this.onOpen();
} else { } else {