當前位置: 首頁>>代碼示例>>Java>>正文


Java Socket類代碼示例

本文整理匯總了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]));
            }
        }
    });
}
 
開發者ID:joshuamarquez,項目名稱:sails.io.java,代碼行數:24,代碼來源:SailsSocket.java

示例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();
}
 
開發者ID:BackPackerDz,項目名稱:PotRoom,代碼行數:26,代碼來源:TalkActivity.java

示例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();
}
 
開發者ID:fahidnasir,項目名稱:socket.io-android-sample,代碼行數:22,代碼來源:MainFragment.java

示例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);
}
 
開發者ID:fahidnasir,項目名稱:socket.io-android-sample,代碼行數:17,代碼來源:MainFragment.java

示例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);
    }
}
 
開發者ID:WojciechZankowski,項目名稱:iextrading4j,代碼行數:18,代碼來源:SocketManager.java

示例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();
}
 
開發者ID:BoxtrotStudio,項目名稱:amazon-gamelift-serversdk-java,代碼行數:27,代碼來源:ServerState.java

示例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();
    }
}
 
開發者ID:nerlihmax,項目名稱:YetAnotherMMORPG,代碼行數:18,代碼來源:Game.java

示例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();
    }
 
開發者ID:Alex-Jerry,項目名稱:LLApp,代碼行數:22,代碼來源:ChatFragment.java

示例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();
    }

}
 
開發者ID:elloza,項目名稱:ArduinoBLEAndroid,代碼行數:25,代碼來源:SocketIOManager.java

示例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();
}
 
開發者ID:joshuamarquez,項目名稱:sails.io.java,代碼行數:19,代碼來源:SailsSocketTest.java

示例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();
}
 
開發者ID:joshuamarquez,項目名稱:sails.io.java,代碼行數:17,代碼來源:SailsSocketTest.java

示例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();
}
 
開發者ID:joshuamarquez,項目名稱:sails.io.java,代碼行數:23,代碼來源:SailsSocketTest.java

示例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();
}
 
開發者ID:joshuamarquez,項目名稱:sails.io.java,代碼行數:23,代碼來源:SailsSocketTest.java

示例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();
}
 
開發者ID:joshuamarquez,項目名稱:sails.io.java,代碼行數:19,代碼來源:SingletonTest.java

示例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);
}
 
開發者ID:saihou,項目名稱:SoLo,代碼行數:21,代碼來源:ChatActivity.java


注:本文中的io.socket.client.Socket類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。