java实现socket.io客户端功能

基于java做一个socket.io客户端

前言

最近公司这边让我去订阅一个第三方机构的websocket server,也是头疼,免不了和对方各种沟通,大家都很忙,收到回复很慢,开发方向也不知道。先是做了一个普通websocket的客户端,但后面了解到对方是基于socket.io做的一个server,又重新做了一个基于socket.io-client-java开源库的客户端。涉及到公司商业机密,所以做了一个demo,转自https://blog.csdn.net/q56231293811/article/details/84873776

1. Client(Socket.IO Client Library for Java)

先上代码

package com.dasnnj.practice.share.socket;

import io.socket.client.IO;
import io.socket.client.Socket;

import java.util.Arrays;

/**
 * Description 

TODO : socket.io client端

* * @author DASNNJ [email protected] * @date 2019-04-27 18:32 */ public class Client { public static void main(String[] args) { String url = "http://localhost:9999"; try { IO.Options options = new IO.Options(); options.transports = new String[]{"websocket"}; //失败重试次数 options.reconnectionAttempts = 10; //失败重连的时间间隔 options.reconnectionDelay = 1000; //连接超时时间(ms) options.timeout = 500; final Socket socket = IO.socket(url, options); //监听自定义msg事件 socket.on("msg", objects -> System.out.println("client: 收到msg->" + Arrays.toString(objects))); //监听自定义订阅事件 socket.on("sub", objects -> System.out.println("client: " + "订阅成功,收到反馈->" + Arrays.toString(objects))); socket.on(Socket.EVENT_CONNECT, objects -> { socket.emit("sub", "我是訂閲對象"); System.out.println("client: " + "连接成功"); }); socket.on(Socket.EVENT_CONNECTING, objects -> System.out.println("client: " + "连接中")); socket.on(Socket.EVENT_CONNECT_TIMEOUT, objects -> System.out.println("client: " + "连接超时")); socket.on(Socket.EVENT_CONNECT_ERROR, objects -> System.out.println("client: " + "连接失败")); socket.connect(); } catch (Exception ex) { ex.printStackTrace(); } } }

流程:
  1. 启动client会创建scoket,并将uri,options等参数set进去

  2. 监听一些事件(可自定义),也就是将event为key,回调为value,put 进callbacks(其为ConcurrentMap)


    java实现socket.io客户端功能_第1张图片
    socket.on监听事件.png
  3. connect 主动连接

  4. 当连接成功时候会触发Socket.EVENT_CONNECT事件,执行了socket.emit("sub", "我是訂閲對象");订阅操作

2. Server(Socket.IO服务器的开源Java实现。基于Netty服务器框架。)

这边server只监听了sub事件(server监听client emit(提交)的sub事件,而client监听的是server emit(反馈)的sub事件)和断开连接事件,并循环发送广播消息(smg事件)

package com.dasnnj.practice.share.socket;

import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer;

import java.util.Map;

/**
 * Description 

TODO : socket.io server端

* * @author DASNNJ [email protected] * @date 2019-04-27 18:31 */ public class Server { public static void main(String[] args) { Configuration config = new Configuration(); config.setHostname("localhost"); config.setPort(9999); SocketIOServer server = new SocketIOServer(config); server.addConnectListener(client -> { System.out.println("server: " + client.getRemoteAddress() + "客户端连接成功"); }); //添加sub事件 server.addEventListener("sub", String.class, (client, data, ackRequest) -> { String c= client.getRemoteAddress().toString(); //获取客户端url参数 Map params = client.getHandshakeData().getUrlParams(); System.out.println("server: " + c+ ":客户端:订阅成功,订阅信息為->" + data); //sub事件成功反馈 client.sendEvent("sub", c+ "客户端你好,我是服务端,你订阅成功了"); }); //添加客户端断开连接事件 server.addDisconnectListener(client -> { String c= client.getRemoteAddress().toString(); //获取设备ip String clientIp = c.substring(1, c.indexOf(":")); System.out.println("server: " + clientIp + "-------------------------" + "客户端已断开连接"); }); server.start(); int i = 0; while (true) { try { Thread.sleep(1500); //广播消息 i++; server.getBroadcastOperations().sendEvent("msg", "30", "hello" + i, "false"); } catch (InterruptedException e) { e.printStackTrace(); } } } }

3. pom.xml



  4.0.0

  com.dasnnj.practice
  practice
  1.0-SNAPSHOT
  
    
      
        org.apache.maven.plugins
        maven-compiler-plugin
        
          8
          8
        
      
    
  


  
    
      io.socket
      socket.io-client
      1.0.0
    
    
      com.corundumstudio.socketio
      netty-socketio
      1.7.16
    
    
      org.slf4j
      slf4j-nop
      1.7.2
    
  

4. 再次感谢

转自 java netty-socketio 与 io.socket的使用,当时看了这位大佬的教程解决了问题

你可能感兴趣的:(java实现socket.io客户端功能)