All Projects → typ0520 → Bizsocket

typ0520 / Bizsocket

Licence: apache-2.0
异步socket,对一些业务场景做了支持

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Bizsocket

Socketify
Raw TCP and UDP Sockets API on Desktop Browsers
Stars: ✭ 67 (-85.71%)
Mutual labels:  socket, tcp, sockets, socket-io, tcp-server, tcp-client
Oksocket
An blocking socket client for Android applications.
Stars: ✭ 2,359 (+402.99%)
Mutual labels:  tcp, socket, tcp-server, tcp-client, client, socket-io
Easytcp
Simple framework for TCP clients and servers. Focused on performance and usability.
Stars: ✭ 60 (-87.21%)
Mutual labels:  tcp, tcp-server, sockets, tcp-client
tcp-net
Build tcp applications in a stable and elegant way
Stars: ✭ 42 (-91.04%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
Tinytcpserver
A small tcp server working under Mono or .NET (4.0) and provides hooks for handling data exchange with clients (works under mono and .net). Behaviour/protocol/reaction could be specified via custom C# script.
Stars: ✭ 14 (-97.01%)
Mutual labels:  tcp, socket, tcp-server, tcp-client
Simplenet
An easy-to-use, event-driven, asynchronous network application framework compiled with Java 11.
Stars: ✭ 164 (-65.03%)
Mutual labels:  tcp, tcp-server, tcp-client, client
ctsTraffic
ctsTraffic is a highly scalable client/server networking tool giving detailed performance and reliability analytics
Stars: ✭ 125 (-73.35%)
Mutual labels:  tcp, sockets, tcp-server, tcp-client
Simpletcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 99 (-78.89%)
Mutual labels:  tcp, tcp-server, tcp-client, client
SocketIOUnity
A Wrapper for socket.io-client-csharp to work with Unity.
Stars: ✭ 69 (-85.29%)
Mutual labels:  socket, tcp, socket-io, socketio
EasyFileTransfer
An easy way to transfer file with any size on network with tcp protocol.
Stars: ✭ 30 (-93.6%)
Mutual labels:  socket, tcp, tcp-server, tcp-client
SuperSimpleTcp
Simple wrapper for TCP client and server in C# with SSL support
Stars: ✭ 263 (-43.92%)
Mutual labels:  tcp, tcp-server, tcp-client
AsyncTcpClient
An asynchronous variant of TcpClient and TcpListener for .NET Standard.
Stars: ✭ 125 (-73.35%)
Mutual labels:  tcp, tcp-server, tcp-client
cAndroid
cAndroid is tool for control your PC by Android phone
Stars: ✭ 23 (-95.1%)
Mutual labels:  socket, tcp-server, tcp-client
Networksocket
NetworkSocket是一个以中间件(middleware)扩展通讯协议,以插件(plug)扩展服务器功能的支持SSL安全传输的通讯框架;目前支持http、websocket、fast、flex策略与silverlight策略协议。
Stars: ✭ 435 (-7.25%)
Mutual labels:  tcp, socket, client
Hprose Nodejs
Hprose is a cross-language RPC. This project is Hprose 2.0 for Node.js
Stars: ✭ 297 (-36.67%)
Mutual labels:  tcp, tcp-server, tcp-client
socket.io-client-core
High-Performance Socket.IO client in C#
Stars: ✭ 70 (-85.07%)
Mutual labels:  socket, socket-io, socketio
Netcat
💻 Netcat client and server modules written in pure Javascript for Node.js.
Stars: ✭ 315 (-32.84%)
Mutual labels:  tcp, socket, client
RRQMSocket
TouchSocket是.Net(包括 C# 、VB.Net、F#)的一个整合性的、超轻量级的网络通信框架。包含了 tcp、udp、ssl、http、websocket、rpc、jsonrpc、webapi、xmlrpc等一系列的通信模块。一键式解决 TCP 黏分包问题,udp大数据包分片组合问题等。使用协议模板,可快速实现「固定包头」、「固定长度」、「区间字符」等一系列的数据报文解析。
Stars: ✭ 286 (-39.02%)
Mutual labels:  socket, tcp, socket-io
network
exomia/network is a wrapper library around System.Socket for easy and fast TCP/UDP client & server communication.
Stars: ✭ 18 (-96.16%)
Mutual labels:  tcp, tcp-server, tcp-client
Message Io
Event-driven message library for building network applications easy and fast.
Stars: ✭ 321 (-31.56%)
Mutual labels:  tcp, tcp-server, sockets

bizsocket

license

Download

异步socket,对一些业务场景做了支持

  • 断线重连
  • 一对一请求
  • 通知、粘性通知
  • 串行请求合并
  • 包分片处理(AbstractFragmentRequestQueue)
  • 缓存
  • 拦截器
  • 支持rxjava,提供类似于retrofit的支持
  • 提供rxjava和rxjava2两种使用方式

使用方式

RxJava2

Maven

<dependency>
  <groupId>com.github.typ0520</groupId>
  <artifactId>bizsocket-rx2</artifactId>
  <version>2.0.0</version>
</dependency>

or Gradle

buildscript {
	repositories {
	   jcenter()
	}
}
 
dependencies {
	compile 'com.github.typ0520:bizsocket-rx2:2.0.0'
}

RxJava

Maven

<dependency>
  <groupId>com.github.typ0520</groupId>
  <artifactId>bizsocket-rx1</artifactId>
  <version>2.0.0</version>
</dependency>

or Gradle

buildscript {
	repositories {
	   jcenter()
	}
}
 
dependencies {
	compile 'com.github.typ0520:bizsocket-rx1:2.0.0'
}

适用协议

如果想使用此库,客户端和服务器的通讯协议中必须要有命令号、包序列号这两个字段

  • 命令号代表请求类型,可以想象成http中url的作用
  • 包序列号是数据包的唯一索引,客户端发起请求时为数据包生成一个唯一索引,服务器返回请求对应的结果时把这个包序列号带回去

协议可以类似于下面这种:

cmd packetId contentLength content
int int int byte[]

也可以类似于下面这样的每个数据包都是一段json字符串,包与包之间用换行符分割

{"cmd": xxx , "packetId": xxx , ...... } 

数据包的创建是通过这两个抽象类PacketFactory、Packet,整个库的数据流转都是通过命令号、包序列号这两个字段来完成的,字段名、出现的位置以及形式不限,只要有这两个字段就适用此库

配置BizSocket

sample中client与server之间的通讯协议是

length(int) cmd(int) seq(int) content(byte[])
数据包的总长度 命令号 数据包的唯一索引 报文体,可以想象成http协议中的body

下面的代码片段来自sample,建议把代码拉下来看

  • 1、 首先需要创建一个数据包类继承自Packet
public class SamplePacket extends Packet {
    static volatile int currentSeq = 0;
    public int length;
    public int cmd;
    public int seq;
    public String content;
    
    @Override
    public int getCommand() {
    	 //覆盖父类的抽象方法
        return cmd;
    }

    @Override
    public String getPacketID() {
        //覆盖父类的抽象方法
        return String.valueOf(seq);
    }
    
    //获取请求数据包byte[],写给服务器
    public byte[] toBytes() {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedSink bufferedSink = Okio.buffer(Okio.sink(bos));
        try {
            //包长 = 内容长度 + 包头固定的12个字节
            ByteString byteString = ByteString.encodeUtf8(content);
            bufferedSink.writeInt(byteString.size() + 12);
            bufferedSink.writeInt(cmd);
            bufferedSink.writeInt(seq);
            bufferedSink.write(byteString);
            bufferedSink.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bos.toByteArray();
    }
}
  • 2、创建PacketFactory,主要用来从流中解析出server发给client的数据包
public class SamplePacketFactory extends PacketFactory {
    @Override
    public Packet getRequestPacket(Packet reusable,Request request) {
        return new SamplePacket(request.command(),request.body());
    }

    @Override
    public Packet getHeartBeatPacket(Packet reusable) {
        return new SamplePacket(SampleCmd.HEARTBEAT.getValue(), ByteString.encodeUtf8("{}"));
    }

    @Override
    public Packet getRemotePacket(Packet reusable,BufferedSource source) throws IOException {
      	 SamplePacket packet = new SamplePacket();
        packet.length = reader.readInt();
        packet.cmd = reader.readInt();
        packet.seq = reader.readInt();
        //减去协议头的12个字节长度
        packet.content = reader.readString(packet.length - 12, Charset.forName("utf-8"));
        return packet;
    }
}
  • 3、配置client
public class SampleClient extends AbstractBizSocket {
    public SampleClient(Configuration configuration) {
        super(configuration);
    }

    @Override
    protected PacketFactory createPacketFactory() {
        return new SamplePacketFactory();
    }
}
  • 3、启动client,以j2se为例写一个main方法
public static void main(String[] args) {
        SampleClient client = new SampleClient(new Configuration.Builder()
                .host("127.0.0.1")
                .port(9103)
                .readTimeout(TimeUnit.SECONDS,30)
                .heartbeat(60)
                .build());

        client.getInterceptorChain().addInterceptor(new Interceptor() {
            @Override
            public boolean postRequestHandle(RequestContext context) throws Exception {
                System.out.println("发现一个请求postRequestHandle: " + context);
                return false;
            }

            @Override
            public boolean postResponseHandle(int command, Packet responsePacket) throws Exception {
                System.out.println("收到一个包postResponseHandle: " + responsePacket);
                return false;
            }
        });

        try {
            //连接
            client.connect();
            //启动断线重连
            client.getSocketConnection().bindReconnectionManager();
            //开启心跳
            client.getSocketConnection().startHeartBeat();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //注册通知,接收服务端的推送
        client.subscribe(client, SampleCmd.NOTIFY_PRICE.getValue(), new ResponseHandler() {
            @Override
            public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) {
                System.out.println("cmd: " + command + " ,requestBody: " + requestBody + " responsePacket: " + responsePacket);
            }

            @Override
            public void sendFailureMessage(int command, Throwable error) {
                System.out.println(command + " ,err: " + error);
            }
        });

	//发起一对一请求
        String json = "{\"productId\" : \"1\",\"isJuan\" : \"0\",\"type\" : \"2\",\"sl\" : \"1\"}";
        client.request(new Request.Builder().command(SampleCmd.CREATE_ORDER.getValue()).utf8body(json).build(), new ResponseHandler() {
            @Override
            public void sendSuccessMessage(int command, ByteString requestBody, Packet responsePacket) {
                System.out.println("cmd: " + command + " ,requestBody: " + requestBody + " attach: " + " responsePacket: " + responsePacket);
            }

            @Override
            public void sendFailureMessage(int command, Throwable error) {
                System.out.println(command + " ,err: " + error);
            }
        });

	//如果想用rxjava的形式调用也是支持的,提供了类似于retrofit通过动态代理创建的service类来调用
        BizSocketRxSupport rxSupport = new BizSocketRxSupport.Builder()
                .requestConverter(new JSONRequestConverter())
                .responseConverter(new JSONResponseConverter())
                .bizSocket(client)
                .build();
        SampleService service = rxSupport.create(SampleService.class);

        JSONObject params = new JSONObject();
        try {
            params.put("pageSize","10000");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //rxjava范例,使用rxjava2需修改Subscriber
        service.queryOrderList(params).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(JSONObject jsonObject) {
                System.out.println("rx response: " + jsonObject);
            }
        });

	//阻塞主线程,防止程序退出,可以想象成android中的Looper类
        while (true) {
            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].