创建handler

/**
 * WebSocket
 */
public class DemoSocketHandler implements WebSocketHandler {

    /**
     * 有连接进来时调用
     */
    @Override
    public void onOpen(WebSocketSession webSocketSession) {
        System.out.println("连接了websocket");
    }

    /**
     * 连接断开时调用
     */
    @Override
    public void onClose(WebSocketSession webSocketSession) {
        System.out.println("关闭了websocket");
    }

    /**
     * 有消息进来时调用
     */
    @Override
    public void onMessage(String message, WebSocketSession webSocketSession) {
        System.out.println("收到了消息"+message);
        try{
            webSocketSession.send("我收到了");
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

在创建http服务的时候,将这个handler加进去

Magician.createTCPServer()
        .handler("/", new DemoRequestHandler())
        // 注意这句
        .webSocketHandler("/websocket", new DemoSocketHandler())
        .bind(8080);

results matching ""

    No results matching ""