即时通讯资料搜集

用到的知识,socket.iosocket.io.java,webRTCwebRTCdemo
Android 基于Socket.io即时通讯IM实现,WebRTC实现视频通话

地址戳这里

描述:(node.js+socket.io+express)后台 + socket.io-client-java(android端) + socket.io-client-swift(ios端) 搭建全平台聊天系统
戳这里

socket.io的api,也就是engine.io

github地址戳这里

读源码时,基本方法了解,socket.emit:向另一端发送消息,socket.on,接收另一端发来的消息。

参考:https://github.com/socketio/socket.io-client-java的demo

Usage

Socket.IO-client Java has almost the same api and features with the original JS client. You use IO#socket to initialize Socket:

socket = IO.socket("http://localhost");
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {

  @Override
  public void call(Object... args) {
    socket.emit("foo", "hi");
    socket.disconnect();
  }

}).on("event", new Emitter.Listener() {

  @Override
  public void call(Object... args) {}

}).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {

  @Override
  public void call(Object... args) {}

});
socket.connect();

This Library uses org.json to parse and compose JSON strings:

// Sending an object
JSONObject obj = new JSONObject();
obj.put("hello", "server");
obj.put("binary", new byte[42]);
socket.emit("foo", obj);

// Receiving an object
socket.on("foo", new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    JSONObject obj = (JSONObject)args[0];
  }
});

Options are supplied as follows:

IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.reconnection = false;

socket = IO.socket("http://localhost", opts);

You can supply query parameters with the query option. NB: if you don't want to reuse a cached socket instance when the query parameter changes, you should use the forceNew option, the use case might be if your app allows for a user to logout, and a new user to login again:

IO.Options opts = new IO.Options();
opts.forceNew = true;
opts.query = "auth_token=" + authToken;
Socket socket = IO.socket("http://localhost", opts);

You can get a callback with Ack when the server received a message:

socket.emit("foo", "woot", new Ack() {
  @Override
  public void call(Object... args) {}
});

And vice versa:

// ack from client to server
socket.on("foo", new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    Ack ack = (Ack) args[args.length - 1];
    ack.call();
  }
});

SSL (HTTPS, WSS) settings:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
  .hostnameVerifier(myHostnameVerifier)
  .sslSocketFactory(mySSLContext.getSocketFactory(), myX509TrustManager)
  .build();

// default settings for all sockets
IO.setDefaultOkHttpWebSocketFactory(okHttpClient);
IO.setDefaultOkHttpCallFactory(okHttpClient);

// set as an option
opts = new IO.Options();
opts.callFactory = okHttpClient;
opts.webSocketFactory = okHttpClient;
socket = IO.socket("https://localhost", opts);

See the Javadoc for more details.

http://socketio.github.io/socket.io-client-java/apidocs/

Transports and HTTP Headers

You can access transports and their HTTP headers as follows.

// Called upon transport creation.
socket.io().on(Manager.EVENT_TRANSPORT, new Emitter.Listener() {
  @Override
  public void call(Object... args) {
    Transport transport = (Transport)args[0];

    transport.on(Transport.EVENT_REQUEST_HEADERS, new Emitter.Listener() {
      @Override
      public void call(Object... args) {
        @SuppressWarnings("unchecked")
        Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
        // modify request headers
        headers.put("Cookie", Arrays.asList("foo=1;"));
      }
    });

    transport.on(Transport.EVENT_RESPONSE_HEADERS, new Emitter.Listener() {
      @Override
      public void call(Object... args) {
        @SuppressWarnings("unchecked")
        Map<String, List<String>> headers = (Map<String, List<String>>)args[0];
        // access response headers
        String cookie = headers.get("Set-Cookie").get(0);
      }
    });
  }
});


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页