当前位置: 首页>>代码示例>>Java>>正文


Java WebSocket类代码示例

本文整理汇总了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) {}
        }
    };
}
 
开发者ID:mervinderuiter,项目名称:home-assistant-android,代码行数:18,代码来源:HassEntities.java

示例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;
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:19,代码来源:WebSocketTransport.java

示例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);
                }
            });
        }
    });
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:27,代码来源:WebSocketTests.java

示例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));
}
 
开发者ID:jacklongway,项目名称:LiteSDK,代码行数:20,代码来源:WebSocketTests.java

示例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);
        }
    });
}
 
开发者ID:xserv,项目名称:xserv-android,代码行数:24,代码来源:Xserv.java

示例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;
}
 
开发者ID:amartinz,项目名称:DeviceControl,代码行数:21,代码来源:ServerWrapper.java

示例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);
    }
}
 
开发者ID:SecUSo,项目名称:privacy-friendly-werewolf,代码行数:15,代码来源:WebSocketServerHandler.java

示例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);
                        }
                    });
                }
            });
}
 
开发者ID:razem-io,项目名称:Steemit-App,代码行数:31,代码来源:MainActivity.java

示例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();
}
 
开发者ID:delletenebre,项目名称:SerialManager,代码行数:11,代码来源:ConnectionService.java

示例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);
  }
}
 
开发者ID:blad,项目名称:reactive-exchange,代码行数:11,代码来源:CoinbaseWebsocket.java

示例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);
}
 
开发者ID:gautamk,项目名称:lankeys,代码行数:32,代码来源:MainActivity.java

示例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();
                        }
                    }
                });
    }
}
 
开发者ID:xserv,项目名称:xserv-android,代码行数:39,代码来源:Xserv.java

示例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();
    }
}
 
开发者ID:omerjerk,项目名称:RemoteDroid,代码行数:30,代码来源:ClientActivity.java

示例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();
        }
    });
}
 
开发者ID:omerjerk,项目名称:RemoteDroid,代码行数:40,代码来源:ServerService.java

示例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);
							}
						}
					});
				}
			});
}
 
开发者ID:y12studio,项目名称:bkbc-bitcoinpos,代码行数:30,代码来源:AddressBlockChainWatcher.java


注:本文中的com.koushikdutta.async.http.WebSocket类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。