重回起点

导航

 
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);
		}
	
	}
}

 

posted on 2019-01-23 09:31  重回起点  阅读(3365)  评论(0编辑  收藏  举报