本文整理匯總了Java中io.socket.client.Socket類的典型用法代碼示例。如果您正苦於以下問題:Java Socket類的具體用法?Java Socket怎麽用?Java Socket使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
Socket類屬於io.socket.client包,在下文中一共展示了Socket類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: emitFrom
import io.socket.client.Socket; //導入依賴的package包/類
/**
* Private method used by {@link SailsSocket#request}
*
* @param request {@link SailsSocketRequest}
*/
private void emitFrom(Socket socket, SailsSocketRequest request) {
// Name of the appropriate socket.io listener on the server
// ( === the request method or "verb", e.g. 'get', 'post', 'put', etc. )
String sailsEndpoint = request.getMethod();
// Since Listener is embedded in request, retrieve it.
final SailsSocketResponse.Listener listener = request.getListener();
socket.emit(sailsEndpoint, request.toJSONObject(), new Ack() {
@Override
public void call(Object... args) {
// Send back jsonWebSocketResponse
if (listener != null) {
listener.onResponse(new JWR((JSONObject) args[0]));
}
}
});
}
示例2: onDestroy
import io.socket.client.Socket; //導入依賴的package包/類
@Override
protected void onDestroy() {
if (mRxAudioPlayer != null) {
mRxAudioPlayer.stopPlay();
}
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer = null;
}
mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT, onConnect);
mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.off(KEY_NEW_MESSAGE, onNewMessage);
mSocket.off(KEY_JOIN_ROOM, onJoinRoom);
mSocket.off(KEY_LEFT_ROOM, onLeftRoom);
mSocket.off(KEY_NUMBER_USERS, onNumberUsers);
mSocket.off(KEY_CONNECTED_USERS, onConnectedUsers);
super.onDestroy();
}
示例3: onCreate
import io.socket.client.Socket; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
ChatApplication app = (ChatApplication) getActivity().getApplication();
mSocket = app.getSocket();
mSocket.on(Socket.EVENT_CONNECT,onConnect);
mSocket.on(Socket.EVENT_DISCONNECT,onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on("new message", onNewMessage);
mSocket.on("user joined", onUserJoined);
mSocket.on("user left", onUserLeft);
mSocket.on("typing", onTyping);
mSocket.on("stop typing", onStopTyping);
mSocket.connect();
startSignIn();
}
示例4: onDestroy
import io.socket.client.Socket; //導入依賴的package包/類
@Override
public void onDestroy() {
super.onDestroy();
mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT, onConnect);
mSocket.off(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.off("new message", onNewMessage);
mSocket.off("user joined", onUserJoined);
mSocket.off("user left", onUserLeft);
mSocket.off("typing", onTyping);
mSocket.off("stop typing", onStopTyping);
}
示例5: subscribe
import io.socket.client.Socket; //導入依賴的package包/類
public <T> void subscribe(final SocketRequest<T> request, final Consumer<T> consumer) {
final String url = createURL(request);
try {
if (socketStore.containsKey(request)) {
return;
}
final Socket socket = socketWrapper.socket(url).connect();
socket.emit("subscribe", request.getParams())
.on("message", args -> processResponse(args, request, consumer));
socketStore.put(request, socket);
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
示例6: initNetworking
import io.socket.client.Socket; //導入依賴的package包/類
GenericOutcome initNetworking() {
if (!networkInitialized) {
String url = "http://" + HOSTNAME + ":" + PORT;
String queryString = PID_KEY + "=" + getProcessID() + "&" +
SDK_VERSION_KEY + "=" + GameLiftServerAPI.SDK_VERSION + "&" +
FLAVOR_KEY + "=" + FLAVOR;
IO.Options options = new IO.Options();
options.query = queryString;
options.reconnection = false;
options.transports = new String[] { "websocket" };
try {
Socket socket = IO.socket(url, options);
sender = new AuxProxyMessageSender(socket);
network = new Network(socket, this);
GenericOutcome result = network.connect();
networkInitialized = result.isSuccessful();
return result;
} catch (URISyntaxException e) {
return new GenericOutcome(new GameLiftError(GameLiftErrorType.LOCAL_CONNECTION_FAILED, e));
}
}
return new GenericOutcome();
}
示例7: connectToServer
import io.socket.client.Socket; //導入依賴的package包/類
private void connectToServer(final String nickname) {
try {
socket = IO.socket("http://localhost:5000");
socket.connect();
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
System.out.println("Connected: "+socket.id());
socket.emit("login", nickname);
}
});
socket.connect();
} catch (Exception e) {
e.printStackTrace();
}
}
示例8: onCreate
import io.socket.client.Socket; //導入依賴的package包/類
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
App app = (App) getActivity().getApplication();
mSocket = app.getSocket();
mSocket.on(Socket.EVENT_CONNECT,onConnect);
mSocket.on(Socket.EVENT_DISCONNECT,onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on("new message", onNewMessage);
mSocket.on("user joined", onUserJoined);
mSocket.on("user left", onUserLeft);
mSocket.on("typing", onTyping);
mSocket.on("stop typing", onStopTyping);
mSocket.connect();
// startSignIn();
}
示例9: connect
import io.socket.client.Socket; //導入依賴的package包/類
@Override
public void connect() {
try {
socket = IO.socket(Constants.SOCKET_URL);
socket.on(Socket.EVENT_CONNECT, args -> {
connectionWithBackend = true;
connectionRelay.call(true);
})
.on(COMMAND_MESSAGE, args -> {
messageRelay.call(gson.fromJson((String) args[0], CommandMessage.class));
})
.on(Socket.EVENT_DISCONNECT, args -> {
connectionRelay.call(false);
connectionWithBackend = false;
});
socket.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
示例10: connect
import io.socket.client.Socket; //導入依賴的package包/類
@Test(timeout = TIMEOUT)
public void connect() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
// Get global sails socket
TestSailsSocketSingleton.getInstance().setUrl(url);
SailsSocket sailsSocket = TestSailsSocketSingleton.getInstance().socket();
sailsSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
values.offer("done");
}
});
sailsSocket.connect();
values.take();
sailsSocket.disconnect();
}
示例11: isConnectedShouldReturnTrue
import io.socket.client.Socket; //導入依賴的package包/類
@Test(timeout = TIMEOUT)
public void isConnectedShouldReturnTrue() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
final SailsSocket sailsSocket = new SailsSocket(url);
sailsSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat(sailsSocket.isConnected(), is(true));
values.offer("done");
}
});
sailsSocket.connect();
values.take();
sailsSocket.disconnect();
}
示例12: shouldGetErrorWhenSettingSocketUrl
import io.socket.client.Socket; //導入依賴的package包/類
@Test(timeout = TIMEOUT)
public void shouldGetErrorWhenSettingSocketUrl() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
TestSailsSocketSingleton.getInstance().setUrl(url);
SailsSocket sailsSocket = TestSailsSocketSingleton.getInstance().socket();
sailsSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
TestSailsSocketSingleton.getInstance().setUrl("http://127.0.0.1:" + PORT);
} catch (Exception e) {
assertThat(e.getMessage(), is("Can not change url while socket is connected"));
values.offer("done");
}
}
});
sailsSocket.connect();
values.take();
sailsSocket.disconnect();
}
示例13: shouldGetErrorWhenSettingSocketOptions
import io.socket.client.Socket; //導入依賴的package包/類
@Test(timeout = TIMEOUT)
public void shouldGetErrorWhenSettingSocketOptions() throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
TestSailsSocketSingleton.getInstance().setUrl(url);
SailsSocket sailsSocket = TestSailsSocketSingleton.getInstance().socket();
sailsSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
try {
TestSailsSocketSingleton.getInstance().setOptions(new IO.Options());
} catch (Exception e) {
assertThat(e.getMessage(), is("Can not change options while socket is connected"));
values.offer("done");
}
}
});
sailsSocket.connect();
values.take();
sailsSocket.disconnect();
}
示例14: socketConnection
import io.socket.client.Socket; //導入依賴的package包/類
@Test
public void socketConnection () throws Exception {
final BlockingQueue<Object> values = new LinkedBlockingQueue<Object>();
testSailsSocketSingleton.socket().on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override
public void call(Object... args) {
assertThat(testSailsSocketSingleton.socket().isConnected(), is(true));
anotherSailsSocketSingleton.setUrl("http://localhost");
assertThat(anotherSailsSocketSingleton.socket().isConnected(), is(false));
values.offer("done");
}
});
testSailsSocketSingleton.socket().connect();
values.take();
testSailsSocketSingleton.socket().disconnect();
}
示例15: onDestroy
import io.socket.client.Socket; //導入依賴的package包/類
@Override
public void onDestroy() {
super.onDestroy();
// clean up mMessagesChat
mMessagesChat = new ArrayList<NewsItem>();
// Leave the room
mSocket.emit("leave", newData);
Log.v("test onDestroy", newData.toString());
// disconnect and drop all subscription
mSocket.emit("disconnect request");
mSocket.disconnect();
mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.off("send room message", onNewMessage);
mSocket.off("joined room", onJoinRoom);
mSocket.off("left room", onLeftRoom);
}