Bootstrap

Java调用WebSocket的服务端和客户端

服务端实现

原生注解

  • 导入依赖文件
  • 需要一个配置类
  • 一个Server类
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-websocket</artifactId>
 </dependency>

// 配置类
@Configuration
@EnableWebSocket
public class WSConfig {

    @Bean
    public ServerEndpointExporter serverEndpoint() {
        return new ServerEndpointExporter();
    }
}
 
// Server 类
@ServerEndpoint("/ws")
@Component
public class WsServerEndpoint {

    @OnOpen
    public void onOpen(Session session) {
        System.out.println("链接成功");
    }

    @OnClose
    public void onClose(Session session) {
        System.out.println("链接关闭");
    }

    @OnMessage
    public void onMessage(Session session, String text) throws IOException {
        System.out.println("收到消息:" + text);
        session.getBasicRemote().sendText("hello world");
    }
}

Spring封装中导入WebSocket

  • 导入依赖
  • 创建 handler
  • 创建 interceptor
  • 创建 配置类
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
// handler 类

package com.go.WebSocketDemo;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.WebSocketMessage;
import org.springframework.web.socket.WebSocketSession;

/**
 * @author husky
 * @data 2022/7/11
 * @filename WebSocketHandler
 */
@Component
public class MyWebSocketHandler implements WebSocketHandler {

    /**
     * 建立连接
     * @param session
     * @throws Exception
     */
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        // 缓存用户信息: userInfo
        System.out.println("hello  user");
    }

    /**
     * 接收消息
     * @param session
     * @param message
     * @throws Exception
     */
    @Override
    public void handleMessage(WebSocketSession session, WebSocketMessage<?> message) throws Exception {


    }

    /**
     * 发生错误
     * @param session
     * @param exception
     * @throws Exception
     */
    @Override
    public void handleTransportError(WebSocketSession session, Throwable exception) throws Exception {


        // 清除用户缓存信息
    }

    /**
     * 关闭连接
     * @param session
     * @param closeStatus
     * @throws Exception
     */
    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus closeStatus) throws Exception {


        // 清除用户缓存信息
    }

    @Override
    public boolean supportsPartialMessages() {
        return false;
    }
}

// interceptor

package com.go.WebSocketDemo;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

/**
 * @author husky
 * @data 2022/7/11
 * @filename MyWebSocketInterceptors
 */
@Component
public class MyWebSocketInterceptors implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        // TODO
        System.out.println("hello hands");
        /*
        处可以做一些处理,例如校验连接中的参数,保存连接用户信息等,
        用户信息等有用信息可存储在 Map<String, Object> attributes 中,
        在 handler 中可使用 WebSocketSession.getAttributes() 方法取出相应的数据。

         */

        返回 false 会导致连接失败
        return true; // [1]
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}

// 配置类
package com.go.WebSocketDemo.Config;

import com.go.WebSocketDemo.MyWebSocketHandler;
import com.go.WebSocketDemo.MyWebSocketInterceptors;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import javax.annotation.Resource;
import javax.websocket.server.ServerEndpoint;
import java.nio.channels.ServerSocketChannel;

/**
 * @author husky
 * @data 2022/7/8
 * @filename WebSocketConfig
 *
 * websocket 的配置类
 *
 */

@Configuration
@EnableWebSocket
// @Service
public class WebSocketConfig implements WebSocketConfigurer {

    // @Bean
    // public ServerEndpointExporter serverEndpointExporter() {
    //     return new ServerEndpointExporter();
    // }
    @Resource
    // MyWebSocketHandler defaultHandler;
    WebSocketHandler defaultHandler;
    @Resource
    MyWebSocketInterceptors defaultInterceptors;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        System.out.println("hello  work");
        registry
                .addHandler(defaultHandler, "ws") // todo  [2]
                .addInterceptors(defaultInterceptors) // todo  [3]
                .setAllowedOrigins("*"); // 解决跨域问题 [4]
    }
}




客户端实现

  • 导入依赖
  • 创建 client
  • 建立链接
<dependency>
    <groupId>org.java-websocket</groupId>
    <artifactId>Java-WebSocket</artifactId>
    <version>1.5.3</version>
</dependency>
package com.go.client;

import java.net.URI;
// import com.alibaba.fastjson.JSONObject;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *  websocket客户端监听类
 * @author 。
 */
public class BaseWebsocketClient extends WebSocketClient {

    private static Logger logger = LoggerFactory.getLogger(BaseWebsocketClient.class);

    public BaseWebsocketClient(URI serverUri) {
        super(serverUri);
    }

    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        logger.info(">>>>>>>>>>>websocket open");

    }

    @Override
    public void onMessage(String s) {
        logger.info(">>>>>>>>>> websocket message");
        System.out.println(s);

    }

    @Override
    public void onClose(int i, String s, boolean b) {
        logger.info(">>>>>>>>>>>websocket close");
    }

    @Override
    public void onError(Exception e) {
        logger.error(">>>>>>>>>websocket error {}",e);
    }


}
public class main {
    public static void main(String[] args) {
        try {
            BaseWebsocketClient myClient = new BaseWebsocketClient(new URI("ws://127.0.0.1:8080/ws"));
            myClient.connect();
            while (!myClient.getReadyState().equals(ReadyState.OPEN)) {
                System.out.println("连接中。。。");
                Thread.sleep(1000);
            }
            // 连接成功往websocket服务端发送数据
            JSONObject object = new JSONObject();
            object.put("message", "success连接");
            myClient.send("是我啊");
            System.out.println("...");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
;