Few day before I talked of a fabulous good library :
http://code.google.com/p/gwt-ws/
This library work very well,so why i talk of it again? In fact, i looked the source, and it looked a bit (just a bit!) complicate for me.Why Do an abstract factory if u don't need it ? I do it at refactoring time, not from begin. Note, it is my way, i don't say it is the good one.
So let's do my way :
WebSocket.java:
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sample.FabiTest.client;
import com.google.gwt.core.client.JavaScriptObject;
/**
* @author senk.christian@googlemail.com
* @date 25.08.2010
* @time 12:04:46
*
* Represents the GWT version of the JavaScripts new WebSocket component.
*/
public class WebSocket
{
/**
* @return <code>True</code> if the WebSocket component is supported by the current browser
*/
public static native boolean IsSupported() /*-{
if ($wnd.WebSocket)
{
return true;
}
else
{
return false;
}
}-*/;
private final JavaScriptObject jsWebSocket;
private final WebSocketCallback socketCallback;
/**
* Creates a new {@link WebSocket} that connects immediately to the end-point URL.
*
* @param url
* @param socketCallback
*/
public WebSocket(final String url, final WebSocketCallback socketCallback)
{
assert url != null;
assert socketCallback != null;
assert IsSupported();
this.socketCallback = socketCallback;
this.jsWebSocket = createJSWebSocket(url, this);
}
public native void send(String message) /*-{
if (message == null)
return;
this.@com.sample.FabiTest.client.WebSocket::socketCallback.send(message);
}-*/;
public native void close() /*-{
this.@com.sample.FabiTest.client.WebSocket::socketCallback.close();
}-*/;
public native int getReadyState() /*-{
return this.@com.sample.FabiTest.client.WebSocket::jsWebSocket.readyState;
}-*/;
public native String getURL() /*-{
return this.@com.sample.FabiTest.client.WebSocket::jsWebSocket.url;
}-*/;
/**
* Creates the JavaScript WebSocket component and set's all callback handlers.
*
* @param url
*/
private native JavaScriptObject createJSWebSocket(final String url, final WebSocket webSocket) /*-{
var jsWebSocket = new WebSocket(url);
jsWebSocket.onopen = function()
{
webSocket.@com.sample.FabiTest.client.WebSocket::onOpen();
}
jsWebSocket.onclose = function()
{
webSocket.@com.sample.FabiTest.client.WebSocket::onClose();
}
jsWebSocket.onerror = function()
{
webSocket.@com.sample.FabiTest.client.WebSocket::onError();
}
jsWebSocket.onmessage = function(socketResponse)
{
if (socketResponse.data)
{
webSocket.@com.sample.FabiTest.client.WebSocket::onMessage(Ljava/lang/String;)(socketResponse.data);
}
}
return jsWebSocket;
}-*/;
private void onOpen()
{
socketCallback.onOpen(this);
}
private void onMessage(String message)
{
socketCallback.onMessage(this, message);
}
private void onError()
{
socketCallback.onError(this);
}
private void onClose()
{
socketCallback.onClose(this);
}
}
WebSocketCallback :
package com.sample.FabiTest.client;
public interface WebSocketCallback {
void onOpen(WebSocket webSocket);
void onClose(WebSocket webSocket);
void onMessage(WebSocket webSocket, String message);
void onError(WebSocket webSocket);
}
As u can see, it is very small footprint; You can use it this way:
phoneGap.initializePhoneGap();
if (!WebSocket.IsSupported())
{
Window.alert("not supported");
return;
}
Window.alert("supporté");
String webSocketURL ="ws://10.0.0.5:8080/websocket";
WebSocket webSocketConnection = new WebSocket(webSocketURL, new WebSocketCallback() {
@Override
public void onOpen(WebSocket webSocket) {
Window.alert("open");
webSocket.send("Hello World");
}
@Override
public void onClose(WebSocket webSocket) {
Window.alert("close");
}
@Override
public void onMessage(WebSocket webSocket, String message) {
Window.alert(message);
}
@Override
public void onError(WebSocket webSocket) {
Window.alert("error");
}
});
Et voila ! Just a simplified vesion of the GWT-WS again.
Simplified socket for iPhone or Android using GWT
Inscription à :
Publier les commentaires (Atom)
Aucun commentaire:
Enregistrer un commentaire