本文整理汇总了Java中com.squareup.okhttp.ws.WebSocket类的典型用法代码示例。如果您正苦于以下问题:Java WebSocket类的具体用法?Java WebSocket怎么用?Java WebSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
WebSocket类属于com.squareup.okhttp.ws包,在下文中一共展示了WebSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onOpen
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onOpen(WebSocket webSocket, Request arg1, Response arg2)
throws IOException {
mWebSocket = webSocket;
setEnvironment(WXEnvironment.getConfig());
WXSDKManager.getInstance().postOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(WXEnvironment.sApplication, "Has switched to DEBUG mode, you can see the DEBUG information on the browser!", Toast.LENGTH_SHORT).show();
}
},0);
for (JSDebuggerCallback callback : mCallbacks.values()) {
callback.onSuccess(arg2);
}
WXLogUtils.e("into--[onOpen]");
}
示例2: close
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@ReactMethod
public void close(int code, String reason, int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// WebSocket is already closed
// Don't do anything, mirror the behaviour on web
FLog.w(
ReactConstants.TAG,
"Cannot close WebSocket. Unknown WebSocket id " + id);
return;
}
try {
client.close(code, reason);
mWebSocketConnections.remove(id);
} catch (Exception e) {
FLog.e(
ReactConstants.TAG,
"Could not close WebSocket connection for id " + id,
e);
}
}
示例3: send
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
/**
* Sends a string over the websocket
*
* @param message the string to send
*/
private void send(String callId, final String message) {
if (message == null) {
throw new RuntimeException("You cannot send `null` messages");
}
try {
Timber.d("-->" + message);
synchronized (mConnection) {
RequestBody request = RequestBody.create(WebSocket.TEXT, message);
mConnection.sendMessage(request);
}
} catch (Exception e) {
final Listener listener = mListeners.remove(callId);
if (listener != null) {
if (listener instanceof ResultListener) {
((ResultListener) listener).onError(new MeteorException(e));
}
}
if (mCallback != null) {
mCallback.onException(e);
}
}
}
示例4: sendMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
public void sendMessage(String message) {
if(!isSupportWebSocket){
return;
}
if (mWebSocket == null) {
return;
}
Buffer messageBuffer = new Buffer();
messageBuffer.writeUtf8(message);
try {
mWebSocket.sendMessage(WebSocket.PayloadType.TEXT, messageBuffer);
} catch (IOException e) {
}
}
示例5: onMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type)
throws IOException {
if (type != WebSocket.PayloadType.TEXT) {
WXLogUtils.w(
"Websocket received unexpected message with payload of type "
+ type);
return;
}
for (JSDebuggerCallback callback : mCallbacks.values()) {
callback.onMessage(payload, type);
}
String message = null;
try {
message = payload.readUtf8();
JSONObject jsonObject = JSONObject.parseObject(message);
Object name = jsonObject.get("method");
Object value = jsonObject.get("arguments");
if (name == null || value == null) {
return;
}
if (TextUtils.equals(name.toString(), "setLogLevel")) {
JSONArray jsonArray = JSONObject.parseArray(value.toString());
String level = jsonArray.get(0).toString();
WXEnvironment.sLogLevel = LogLevel.valueOf(level.toUpperCase());
WXLogUtils.v("into--[onMessage]setLogLevel");
}
} catch (Exception e) {
} finally {
payload.close();
}
}
示例6: onMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
if (type == WebSocket.PayloadType.TEXT) {
String temp = payload.readUtf8();
Log.e(TAG, "into--[onMessage] msg:" + temp);
payload.close();
if (TextUtils.equals("refresh", temp) && mHandler != null) {
mHandler.obtainMessage(Constants.HOT_REFRESH_REFRESH, 0, 0, mUrl).sendToTarget();
}
}
}
示例7: send
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@ReactMethod
public void send(String message, int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// This is a programmer error
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
}
try {
client.sendMessage(
WebSocket.PayloadType.TEXT,
new Buffer().writeUtf8(message));
} catch (IOException | IllegalStateException e) {
notifyWebSocketFailed(id, e.getMessage());
}
}
示例8: sendMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
private void sendMessage(int requestID, String message) {
if (mWebSocket == null) {
triggerRequestFailure(
requestID,
new IllegalStateException("WebSocket connection no longer valid"));
return;
}
Buffer messageBuffer = new Buffer();
messageBuffer.writeUtf8(message);
try {
mWebSocket.sendMessage(WebSocket.PayloadType.TEXT, messageBuffer);
} catch (IOException e) {
triggerRequestFailure(requestID, e);
}
}
示例9: onMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onMessage(BufferedSource payload, WebSocket.PayloadType type) throws IOException {
if (type != WebSocket.PayloadType.TEXT) {
FLog.w(TAG, "Websocket received unexpected message with payload of type " + type);
return;
}
String message = null;
try {
message = payload.readUtf8();
} finally {
payload.close();
}
Integer replyID = null;
try {
JsonParser parser = new JsonFactory().createParser(message);
String result = null;
while (parser.nextToken() != JsonToken.END_OBJECT) {
String field = parser.getCurrentName();
if ("replyID".equals(field)) {
parser.nextToken();
replyID = parser.getIntValue();
} else if ("result".equals(field)) {
parser.nextToken();
result = parser.getText();
}
}
if (replyID != null) {
triggerRequestSuccess(replyID, result);
}
} catch (IOException e) {
if (replyID != null) {
triggerRequestFailure(replyID, e);
} else {
abort("Parsing response message from websocket failed", e);
}
}
}
示例10: sendBinary
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@ReactMethod
public void sendBinary(String base64String, int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// This is a programmer error
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
}
try {
client.sendMessage(
WebSocket.PayloadType.BINARY,
new Buffer().write(ByteString.decodeBase64(base64String)));
} catch (IOException | IllegalStateException e) {
notifyWebSocketFailed(id, e.getMessage());
}
}
示例11: sendMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
public void sendMessage(int requestID, String message) {
if (mWebSocket == null) {
return;
}
Buffer messageBuffer = new Buffer();
messageBuffer.writeUtf8(message);
try {
Log.v(TAG, "sendMessage " + message);
mWebSocket.sendMessage(WebSocket.PayloadType.TEXT, messageBuffer);
} catch (IOException e) {
Log.e(TAG, "sendMessage IOException " + e.toString());
}
}
示例12: onOpen
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onOpen(WebSocket webSocket, Request arg1, Response arg2)
throws IOException {
mWebSocket = webSocket;
if (mConnectCallback != null) {
mConnectCallback.onSuccess(null);
}
}
示例13: write
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
private void write(final Item item) {
new Thread() {
public void run() {
try {
socket.sendMessage(
WebSocket.PayloadType.TEXT,
new Buffer().writeUtf8(item.payload)
);
} catch (IOException e) {
item.channel.onError(e);
}
}
}.start();
}
示例14: onMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onMessage(BufferedSource source, WebSocket.PayloadType payloadType) {
try {
String payload = source.readUtf8();
JSONObject item = new JSONObject(payload);
onMessageAll(new AxEvent(client, item));
source.close();
} catch (IOException e) {
onErrorAll(e);
}
}
示例15: onMessage
import com.squareup.okhttp.ws.WebSocket; //导入依赖的package包/类
@Override
public void onMessage(BufferedSource source, WebSocket.PayloadType type) throws IOException {
String payload = source.readUtf8();
if (payload.contains(pattern)) {
res.set(payload);
lock.countDown();
}
source.close();
}