websocket服务器提供类

 

<!-- websocket -->
			<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-websocket</artifactId>
				<version>2.2.1.RELEASE</version>
			</dependency>
package com.zcdy.dsc.modules.viewplatform.websocket;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.net.URI;

/**
 * @program: siutp-parent
 * @description: 用于连接3d的websocket
 * @author: lyl
 * @create: 2021-11-30 14:54
 **/
@Component
@Slf4j
@ServerEndpoint("/websocket/connect")
public class ViewWebSocket {

    private Session session;


    public ViewWebSocket() {
        super();
    }

    /**
     * @Author: lyl
     * @Date: 2021/11/30 15:19:34
     * @Param: [endpointURI]
     * @Return:
     * @Description: 创建url对应websocket连接
     */
    public ViewWebSocket(URI endpointURI) {
        super();
        try {
            WebSocketContainer container = ContainerProvider.getWebSocketContainer();	// 获得WebSocketContainer
            this.session = container.connectToServer(ViewWebSocket.class, endpointURI);	// 该方法会阻塞
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * @Author: lyl
     * @Date: 2021/11/30 15:26:38
     * @Param: [session]
     * @Return: void
     * @Description: 连接打开触发
     */
    @OnOpen
    public void onOpen(Session session) {
        try {
            this.session = session;
            log.info("【viewWebsocket消息】有新的连接");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Author: lyl
     * @Date: 2021/11/30 15:26:08
     * @Param: []
     * @Return: void
     * @Description: 连接断开处理
     */
    @OnClose
    public void onClose() {
        try {
            if(log.isInfoEnabled()){
                log.info("【viewWebsocket消息】连接断开");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @Author: lyl
     * @Date: 2021/11/30 15:24:06
     * @Param: [session, error]
     * @Return: void
     * @Description: 调用错误处理
     */
    @OnError
    public void onError(Session session, Throwable error) {
        if(log.isErrorEnabled()){
            log.error("WS调用error处理->{}",error.getMessage());
        }
    }

    /**
     * @Author: lyl
     * @Date: 2021/11/30 15:23:48
     * @Param: [message]
     * @Return: void
     * @Description: 接收消息处理
     */
    @OnMessage
    public void onMessage(String message) {
        JSONObject obj = new JSONObject();
        // 业务类型
        obj.put("cmd", "heartcheck");
        // 消息内容
        obj.put("msgTxt", "心跳响应");
        synchronized (session) {
            try {
                session.getBasicRemote().sendText(obj.toJSONString());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    /**
     * @Author: lyl
     * @Date: 2021/11/30 15:23:01
     * @Param: [message] 发送的消息
     * @Return: void
     * @Description: 发送websocket消息
     */
    public void sendMessage(String message) {
        if (session != null && session.isOpen()) {
            try {
                log.info("【ViewWebsocket消息】 :" + message);
                synchronized (session) {
                    session.getBasicRemote().sendText(message);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}

然后使用网页测试socket连接就可以了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值