socket.io客户端java

各语言socket.io 客户端

https://github.com/socketio

  <dependency>
                <groupId>io.socket</groupId>
                <artifactId>socket.io-client</artifactId>
                <version>1.0.0</version>
            </dependency>

package com.pojo.system.config;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pojo.common.core.utils.StringUtils;
import com.pojo.system.domain.SysMessage;
import com.pojo.system.service.SysMessageService;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * Minio 配置信息
 *
 * @author ruoiy
 */
@Configuration
@ConfigurationProperties(prefix = "notice.im")
@Data
public class NoticeIMConfig {
    /**
     * 服务地址
     */
    private String url;
    @Autowired
    SysMessageService sysMessageService;

    @SneakyThrows
    @Bean
    public Socket getSocket() {
        IO.Options options = new IO.Options();
        options.transports = new String[]{"websocket"};
        options.reconnectionAttempts = 2;     // 重连尝试次数
        options.reconnectionDelay = 1000;     // 失败重连的时间间隔(ms)
        options.timeout = 20000;              // 连接超时时间(ms)
        options.forceNew = true;
        options.reconnection = true;
        options.query = "userId=-1&clientType=APP&userName=Server";  //websocket传参
        io.socket.client.Socket instance = IO.socket(url, options);
        instance.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {
                for (Object arg : args) {
                    System.out.println(arg);
                }

            }
        });

        instance.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
            @Override
            public void call(Object... args) {

            }
        });
              //接收事件消息
        instance.on("sysMessageUpdate", new Emitter.Listener() {
            @Override
            public void call(Object... objects) {
                String messg = String.valueOf(objects[0]);
                if (StringUtils.isNotBlank(messg)) {
                    JSONObject jsonObject = JSONObject.parseObject(messg);
                    String hkhiGroupId = jsonObject.getString("hkhjGroupId");
                    Long userId = Long.valueOf(jsonObject.getString("userId"));
                    String message = jsonObject.getString("messgae");
                    QueryWrapper<SysMessage> wrapper = new QueryWrapper<>();
                    wrapper.lambda().eq(SysMessage::getImId, hkhiGroupId)
                            .ne(SysMessage::getReceiveId, userId);
                    SysMessage sysMessage = SysMessage.builder()
                            .msgContent(message).build();
                    sysMessageService.update(sysMessage, wrapper);
                }

            }
        });
        return instance.connect();
    }
}

socket.io服务端url  

 http://127.0.0.1:28889

发送消息


        VideoAiMsg videoAiMsg = VideoAiMsg.builder().id(100L)
                .type("aievent").build();
      
      //  videoai  服务端接收的事件名称     videoAiMsg  消息
         socket.emit("videoai", new JSONObject(videoAiMsg) );

socket.io-client客户端文档

socket.io-client – Emitting events

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值