本文整理汇总了Java中com.koushikdutta.async.http.WebSocket类的典型用法代码示例。如果您正苦于以下问题:Java WebSocket类的具体用法?Java WebSocket怎么用?Java WebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocket类属于com.koushikdutta.async.http包,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getStringCallback
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
private WebSocket.StringCallback getStringCallback() {
return new WebSocket.StringCallback(){
public void onStringAvailable(String s){
try {
final JSONObject row = new JSONObject(s);
Runnable dispatchState = new Runnable() {
@Override
public void run() {
processData(row);
}
};
Handler mainHandler = new Handler(context.getMainLooper());
mainHandler.post(dispatchState);
} catch (JSONException e) {}
}
};
}
示例2: setStringCallback
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
@Override
public void setStringCallback(final StringCallback callback) {
if (this.stringCallback == callback)
return;
if (callback == null) {
this.webSocket.setStringCallback(null);
} else {
this.webSocket.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String s) {
callback.onStringAvailable(s);
}
});
}
this.stringCallback = callback;
}
示例3: setUp
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
@Override
protected void setUp() throws Exception {
super.setUp();
httpServer = new AsyncHttpServer();
httpServer.setErrorCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
fail();
}
});
httpServer.listen(AsyncServer.getDefault(), 5000);
httpServer.websocket("/ws", new WebSocketRequestCallback() {
@Override
public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
webSocket.setStringCallback(new StringCallback() {
@Override
public void onStringAvailable(String s) {
webSocket.send(s);
}
});
}
});
}
示例4: testWebSocket
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
public void testWebSocket() throws Exception {
final Semaphore semaphore = new Semaphore(0);
AsyncHttpClient.getDefaultInstance().websocket("http://localhost:5000/ws", null, new WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
webSocket.send("hello");
webSocket.setStringCallback(new StringCallback() {
@Override
public void onStringAvailable(String s) {
assertEquals(s, "hello");
semaphore.release();
}
});
}
});
assertTrue(semaphore.tryAcquire(TIMEOUT, TimeUnit.MILLISECONDS));
}
示例5: setOtherWsCallback
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
private void setOtherWsCallback(WebSocket ws) {
ws.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception e) {
isConnected = false;
mCallbacks.clear();
onCloseConnection(e);
reConnect();
}
});
ws.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String event) {
manageMessage(event);
}
});
}
示例6: stopServer
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
public void stopServer() {
unregisterReceivers();
if (mServer != null) {
mServer.stop();
mServer = null;
}
if (mServerSocket != null) {
mServerSocket.stop();
mServerSocket = null;
}
for (final WebSocket socket : _sockets) {
if (socket == null) { continue; }
socket.send(ACTION_TERMINATING);
socket.close();
}
_sockets.clear();
isStopped = true;
}
示例7: send
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
/**
* Message to send data packages over the network
*
* @param networkPackage
*/
public void send(NetworkPackage networkPackage) {
Gson gson = new Gson();
String s = gson.toJson(networkPackage);
Log.d(TAG, "Server sent package to all clients: " + s);
for (WebSocket socket : _sockets) {
socket.send(s);
}
}
示例8: fetchData
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
private void fetchData(final String method){
AsyncHttpClient.getDefaultInstance().websocket("wss://this.piston.rocks", "",
new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
if (ex != null) {
ex.printStackTrace();
return;
}
webSocket.send(
"{" +
"\"jsonrpc\": \"2.0\", " +
"\"method\": \"get_discussions_by_" + method + "\", " +
"\"params\": [{\"tag\": \"\", \"limit\": 10, " +
"\"filter_tags\": []}], \"id\": 1" +
"}");
webSocket.setStringCallback(new WebSocket.StringCallback() {
public void onStringAvailable(String s) {
DiscussionResults discussionResults = gson.fromJson(s, DiscussionResults.class);
discussions.clear();
discussions.addAll(discussionResults.getResult());
runOnUiThread(run);
}
});
}
});
}
示例9: stopWebserver
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
private static void stopWebserver() {
if (webSockets.size() > 0) {
for (WebSocket socket : webSockets) {
socket.close();
}
}
webServer.stop();
//AsyncServer.getDefault().stop();
}
示例10: onCompleted
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
@Override public void onCompleted(Exception error, WebSocket webSocket) {
if (error != null) {
for (Subscriber<? super String> subscriber : subscriberList) {
subscriber.onError(error);
}
} else {
webSocket.send(CoinbaseRegistration);
webSocket.setStringCallback(this);
}
}
示例11: onResume
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
@Override
protected void onResume() {
super.onResume();
this.server = new AsyncHttpServer();
server.websocket("/live", new AsyncHttpServer.WebSocketRequestCallback() {
@Override
public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
//Use this to clean up any references to your websocket
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
try {
if (ex != null)
Log.e("WebSocket", "Error");
} finally {
}
}
});
webSocket.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String s) {
toast(s);
}
});
}
});
server.listen(8888);
}
示例12: connect
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
private void connect(boolean no_ar) {
if (!no_ar) {
isAutoReconnect = true;
}
if (!isConnected()) {
String protocol = "";
String port = PORT;
if (isSecure) {
protocol = "s";
port = TLS_PORT;
}
mCallbacks.clear();
AsyncHttpClient as = AsyncHttpClient.getDefaultInstance();
mConn = as.websocket(String.format(
URL, protocol, HOST, port, mAppId, VERSION), null,
new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception e, WebSocket ws) {
if (e == null) {
setOtherWsCallback(ws);
handshake();
} else {
mCallbacks.clear();
onErrorConnection(e);
// eccezione, error socket in connection non e' la error
reConnect();
}
}
});
}
}
示例13: surfaceCreated
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
decoder = MediaCodec.createDecoderByType(CodecUtils.MIME_TYPE);
new Thread(new Runnable() {
@Override
public void run() {
doDecoderThingie();
}
}).start();
AsyncHttpClient.getDefaultInstance().websocket("ws://" + address, null, websocketCallback);
String ip = address.split(":")[0];
showToast("IP = " + ip);
AsyncHttpClient.getDefaultInstance().websocket("ws://" + ip + ":6059", null,
new AsyncHttpClient.WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket tSocket) {
if (ex != null) {
ex.printStackTrace();
showToast(ex.getMessage());
return;
}
touchSocket = tSocket;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
示例14: onConnected
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
@Override
public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) {
_sockets.add(webSocket);
showToast("Someone just connected");
//Start rendering display on the surface and setting up the encoder
if (encoderThread == null) {
startDisplayManager();
encoderThread = new Thread(new EncoderWorker(), "Encoder Thread");
encoderThread.start();
}
//Use this to clean up any references to the websocket
webSocket.setClosedCallback(new CompletedCallback() {
@Override
public void onCompleted(Exception ex) {
try {
if (ex != null)
ex.printStackTrace();
} finally {
_sockets.clear();
}
showToast("Disconnected");
dispose();
}
});
webSocket.setStringCallback(new WebSocket.StringCallback() {
@Override
public void onStringAvailable(String s) {
Log.d(TAG, "String received. No idea what to do with it.");
}
});
webSocket.setDataCallback(new DataCallback() {
@Override
public void onDataAvailable(DataEmitter dataEmitter, ByteBufferList byteBufferList) {
byteBufferList.recycle();
}
});
}
示例15: createWebsocket
import com.koushikdutta.async.http.WebSocket; //导入依赖的package包/类
public void createWebsocket() {
UI.logv("[WEBSOCKET CONNECT TO " + WS_URL + "]");
Future<WebSocket> x = AsyncHttpClient.getDefaultInstance().websocket(WS_URL, null,
new WebSocketConnectCallback() {
@Override
public void onCompleted(Exception ex, WebSocket webSocket) {
if (ex != null) {
ex.printStackTrace();
return;
}
String op = buildOp(TESTONLY_ADDR);
UI.logv("[WEBSOCKET CONNECT] and send " + op);
webSocket.send(op);
webSocket.setStringCallback(new StringCallback() {
@Override
public void onStringAvailable(String json) {
UI.logv("[WEBSOCKET READ]" + json);
BcWsAddrSubBuilder b = new BcWsAddrSubBuilder();
BcWsAddrSub model = b.toModel(json);
List<BcWsAddrSubTxItem> outs = model
.getOutTxs();
for (BcWsAddrSubTxItem bcWsAddrSubTxItem : outs) {
UI.logv("[READ TX]" + bcWsAddrSubTxItem);
}
}
});
}
});
}