SocketIO Client

package com.x061.socketio.demo;

import org.json.JSONObject;

import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;


public class ClientDemo {
	public static void main(String[] args) throws Exception {
		IO.Options options = new IO.Options();
		options.transports = new String[]{"websocket"};
		options.reconnectionAttempts = 2;
		options.reconnectionDelay = 1000;
		
		final Socket socket = IO.socket("http://localhost:5000/", options);
		
		socket.on("test_response", new Emitter.Listener() {
			
			@Override
			public void call(Object... arg0) {
				JSONObject obj = (JSONObject)arg0[0];
				System.out.println(obj.toString());
			}
		});
		
		socket.connect();
		
		JSONObject sendObj = new JSONObject();
		sendObj.put("msg", "dddd");
		socket.emit("test", sendObj);
		
		while(true){
			Thread.sleep(1000);
		}
	
	}
}

猜你喜欢

转载自www.cnblogs.com/flyingcloude/p/10307228.html