Added some exception handling

This commit is contained in:
Animesh Kumar 2011-02-13 00:00:27 +05:30 committed by Fil Maj
parent 83e89909ed
commit 9270d13981
3 changed files with 96 additions and 57 deletions

View File

@ -30,16 +30,14 @@
// WebSocket Object. All listener methods are cleaned up! // WebSocket Object. All listener methods are cleaned up!
var WebSocket = global.WebSocket = function(url) { var WebSocket = global.WebSocket = function(url) {
// must be overloaded
this.onopen = null;
this.onmessage = null;
this.onclose = null;
this.onerror = null;
// get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java) // get a new websocket object from factory (check com.strumsoft.websocket.WebSocketFactory.java)
this.socket = WebSocketFactory.getInstance(url); this.socket = WebSocketFactory.getInstance(url);
// store in registry // store in registry
WebSocket.store[this.socket.getId()] = this; if(this.socket) {
WebSocket.store[this.socket.getId()] = this;
} else {
throw new Error('websocket instantiation failed! Address could be wrong.');
}
}; };
// storage to hold websocket object for later invokation of event methods // storage to hold websocket object for later invokation of event methods
@ -62,6 +60,7 @@
WebSocket.store[evt._target]['onerror'].call(global, evt._data); WebSocket.store[evt._target]['onerror'].call(global, evt._data);
} }
// instance event methods
WebSocket.prototype.send = function(data) { WebSocket.prototype.send = function(data) {
this.socket.send(data); this.socket.send(data);
} }
@ -73,5 +72,23 @@
WebSocket.prototype.getReadyState = function() { WebSocket.prototype.getReadyState = function() {
this.socket.getReadyState(); this.socket.getReadyState();
} }
///////////// Must be overloaded
WebSocket.prototype.onopen = function(){
throw new Error('onopen not implemented.');
};
// alerts message pushed from server
WebSocket.prototype.onmessage = function(msg){
throw new Error('onmessage not implemented.');
};
// alerts message pushed from server
WebSocket.prototype.onerror = function(msg){
throw new Error('onerror not implemented.');
};
// alert close event
WebSocket.prototype.onclose = function(){
throw new Error('onclose not implemented.');
};
})(); })();

View File

@ -43,6 +43,7 @@ import java.util.Set;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import android.util.Log;
import android.webkit.WebView; import android.webkit.WebView;
/** /**
@ -246,17 +247,42 @@ public class WebSocket implements Runnable {
// ////////////////////////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////////////////////////
/** /**
* Starts a new Thread and connects to server * Starts a new Thread and connects to server
*
* @throws IOException
*/ */
public void connect() { public Thread connect() throws IOException {
this.running = true; this.running = true;
(new Thread(this)).start(); this.readyState = WEBSOCKET_STATE_CONNECTING;
// open socket
socketChannel = SocketChannel.open();
socketChannel.configureBlocking(false);
// set address
socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
// start a thread to make connection
// More info:
// http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82
// http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT);
Log.v("websocket", "Starting a new thread to manage data reading/writing");
Thread th = new Thread(this);
th.start();
// return thread object for explicit closing, if needed
return th;
} }
public void run() { public void run() {
try { while (this.running) {
_connect(); try {
} catch (IOException e) { _connect();
this.onError(e); } catch (IOException e) {
this.onError(e);
}
} }
} }
@ -265,7 +291,7 @@ public class WebSocket implements Runnable {
*/ */
public void close() { public void close() {
this.readyState = WebSocket.WEBSOCKET_STATE_CLOSING; this.readyState = WebSocket.WEBSOCKET_STATE_CLOSING;
// close socket channel // close socket channel
try { try {
this.socketChannel.close(); this.socketChannel.close();
@ -274,10 +300,10 @@ public class WebSocket implements Runnable {
} }
this.running = false; this.running = false;
selector.wakeup(); selector.wakeup();
// fire onClose method // fire onClose method
this.onClose(); this.onClose();
this.readyState = WebSocket.WEBSOCKET_STATE_CLOSED; this.readyState = WebSocket.WEBSOCKET_STATE_CLOSED;
} }
@ -289,7 +315,7 @@ public class WebSocket implements Runnable {
*/ */
public void send(String text) { public void send(String text) {
try { try {
if(this.readyState == WEBSOCKET_STATE_OPEN){ if (this.readyState == WEBSOCKET_STATE_OPEN) {
_send(text); _send(text);
} else { } else {
this.onError(new NotYetConnectedException()); this.onError(new NotYetConnectedException());
@ -389,45 +415,31 @@ 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.configureBlocking(false);
socketChannel.connect(new InetSocketAddress(uri.getHost(), port));
// More info:
// http://groups.google.com/group/android-developers/browse_thread/thread/45a8b53e9bf60d82
// http://stackoverflow.com/questions/2879455/android-2-2-and-bad-address-family-on-socket-connect
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("java.net.preferIPv6Addresses", "false");
selector = Selector.open();
socketChannel.register(selector, SelectionKey.OP_CONNECT);
// Continuous loop that is only supposed to end when "close" is called. // Continuous loop that is only supposed to end when "close" is called.
while (this.running) {
selector.select();
Set<SelectionKey> keys = selector.selectedKeys();
Iterator<SelectionKey> i = keys.iterator();
while (i.hasNext()) { selector.select();
SelectionKey key = i.next(); Set<SelectionKey> keys = selector.selectedKeys();
i.remove(); Iterator<SelectionKey> i = keys.iterator();
if (key.isConnectable()) {
if (socketChannel.isConnectionPending()) { while (i.hasNext()) {
socketChannel.finishConnect(); SelectionKey key = i.next();
} i.remove();
socketChannel.register(selector, SelectionKey.OP_READ); if (key.isConnectable()) {
_writeHandshake(); if (socketChannel.isConnectionPending()) {
socketChannel.finishConnect();
} }
if (key.isReadable()) { socketChannel.register(selector, SelectionKey.OP_READ);
try { _writeHandshake();
_read(); }
} catch (NoSuchAlgorithmException nsa) { if (key.isReadable()) {
this.onError(nsa); try {
} _read();
} catch (NoSuchAlgorithmException nsa) {
this.onError(nsa);
} }
} }
} }
} }
private void _writeHandshake() throws IOException { private void _writeHandshake() throws IOException {

View File

@ -26,7 +26,6 @@
package com.phonegap.websocket; package com.phonegap.websocket;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException;
import java.util.Random; import java.util.Random;
import android.webkit.WebView; import android.webkit.WebView;
@ -53,14 +52,25 @@ public class WebSocketFactory {
this.appView = appView; this.appView = appView;
} }
public WebSocket getInstance(String url) throws URISyntaxException { public WebSocket getInstance(String url) {
// use Draft75 by default
return getInstance(url, WebSocket.Draft.DRAFT75); return getInstance(url, WebSocket.Draft.DRAFT75);
} }
public WebSocket getInstance(String url, WebSocket.Draft draft) throws URISyntaxException { public WebSocket getInstance(String url, WebSocket.Draft draft) {
WebSocket socket = new WebSocket(appView, new URI(url), draft, getRandonUniqueId()); WebSocket socket = null;
socket.connect(); Thread th = null;
return socket; try {
socket = new WebSocket(appView, new URI(url), draft, getRandonUniqueId());
th = socket.connect();
return socket;
} catch (Exception e) {
//Log.v("websocket", e.toString());
if(th != null) {
th.interrupt();
}
}
return null;
} }
/** /**