Skip to content

超简单的socket连接工具,拓展性高且使用简单

Notifications You must be signed in to change notification settings

Verlif/socket-point

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Socket Point

socket-pointsocket-core 的升级版,重构了代码,并且统一了监听逻辑。

这一次,你不再需要clientserver了,这一次你需要的只是SocketPoint

特点

  • clientserver的区别,统一为SocketPoint
  • SocketPoint允许接收其他SocketPoint连接或是连接到其他的SocketPoint
  • 与原生Socket或是WebSocket互通(请注意ReceiveHolder消息终止方式)
  • 同一个SocketPoint可以即是server也是client
  • 允许自定义信息传输格式化,多行文本不再会被分成多条信息发送
  • 拓展性更高的工厂模式与监听器,可以支持连接过滤、信息加密、限时连接等多种特性。

实例

import idea.verlif.socketpoint.EndPointException;

public class MainTest {

    public static void main(String[] args) {
        // 开启端点服务器
        SocketPoint socketPoint = new SocketPoint();
        // 设置信息监听器
        socketPoint.setMessageListener((endPoint, message) -> {
            System.out.println(message);
            System.out.println("-----------------------------");
        });
        // 设置连接关闭监听器
        socketPoint.setClosedListener(endPoint ->
                System.out.println("连接被关闭 - " + endPoint.getTarget().getRemoteSocketAddress()));
        // 设置拒绝端点连接监听器,向来访端点发送拒绝信息
        socketPoint.setRejectedListener(endPoint ->
                endPoint.send("连接已达上限! "));
        // 开启端点接收服务
        new Thread(() -> {
            try {
                socketPoint.start(new SocketConfig().max(2).tied(1));
            } catch (IOException e) {
                throw new EndPointException(e);
            }
        }).start();

        // 同时,此端点可以连接到另一个端点,这里是设置的本端点,默认的端口是16508
        SocketPoint.ConnectionHolder link = socketPoint.link("127.0.0.1", 16508);
        // 通过此连接进行信息交互
        link.send("你好呀!");
        // 一个端点可以连接无限个端点,并且共用之前设置的各种监听器
        SocketPoint.ConnectionHolder link2 = socketPoint.link("127.0.0.1", 16508);
        // 通过此连接进行信息交互
        link2.send("你好呀!");
    }
}

此时的控制台打印如下:

你好呀!
-----------------------------
你好呀!
-----------------------------

添加依赖

添加Jitpack仓库源

last-version: Release

maven

<repositories>
   <repository>
       <id>jitpack.io</id>
       <url>https://jitpack.io</url>
   </repository>
</repositories>

Gradle

allprojects {
  repositories {
      maven { url 'https://jitpack.io' }
  }
}

添加依赖

maven

<dependencies>
    <dependency>
        <groupId>com.github.Verlif</groupId>
        <artifactId>socket-point</artifactId>
        <version>last-version</version>
    </dependency>
</dependencies>

Gradle

dependencies {
  implementation 'com.github.Verlif:socket-point:last-version'
}

About

超简单的socket连接工具,拓展性高且使用简单

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages