重回起点

导航

 
package com.fd.socketio;

import org.json.JSONObject;

import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;

import io.socket.client.Socket;

public class MySocketIOServer {
	public static void main(String[] args) {
		Configuration config = new Configuration();
		config.setHostname("localhost");
		config.setPort(9098);
		final SocketIOServer server = new SocketIOServer(config);
		server.addConnectListener(new ConnectListener() {
			@Override
			public void onConnect(SocketIOClient client) {
				String token = client.getHandshakeData().getUrlParams().get("token").get(0);
//				if (!token.equals("87df42a424c48313ef6063e6a5c63297")) {
//					client.disconnect();// 校验token示例
//				}
				System.out.println("sessionId:" + client.getSessionId()
						+ ",token:" + token);
			}
		});
		server.addEventListener(Socket.EVENT_MESSAGE, String.class,
				new DataListener<String>() {
					@Override
					public void onData(SocketIOClient client, String data,
							AckRequest ackSender) throws Exception {
						System.out.println("client data:" + data);
						server.getBroadcastOperations().sendEvent(Socket.EVENT_MESSAGE, "hi");
					}
				});
		server.start();
		
		while(true){
			try{
				JSONObject obj = new JSONObject();
				obj.put("message_type", "place_order");
				
				server.getBroadcastOperations().sendEvent("order_book", obj.toString());
			}catch(Exception e){
				
			}
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
//		try {
//			Thread.sleep(Integer.MAX_VALUE);
//		} catch (InterruptedException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		server.stop();
	}
}

 

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