跳到主要内容
CNode

请问除了用socket.io提供的JS连接服务端?还有其他方式连接吗?

问答
3330216851发布于 12 年前最后回复 12 年前
454910

比如JAVA客户端连接,或者通过URL连接?

查看回复

回复 (4)

3
330216851#412 年前

嗯,楼上说的是

com.github.nkzawa socket.io-client 0.3.0 Or to install it manually, please refer dependencies to pom.xml.

Add it as a gradle dependency for Android Studio, in build.gradle:

compile 'com.github.nkzawa:socket.io-client:0.3.0' 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 get a callback with Ack when the server received a message:

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

// default SSLContext for all sockets IO.setDefaultSSLContext(mySSLContext);

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

B
bnuhero#312 年前
引用 330216851解决了,JAVA可以连接socket.io服务端的。

@330216851 最好能把经验分享给大家,避免他人再走弯路。

3
330216851#212 年前

解决了,JAVA可以连接socket.io服务端的。

Z
zhs077#112 年前

tcp

参与回复

登录后即可参与回复。登录