### 1、发送（像 websocket）

send

```java
public class Demo {
    public static void main(String[] args) throws Throwable {
        //::启动服务端
        SocketD.createServer("sd:ws")
                .config(c -> c.port(8602))
                .start();

        Thread.sleep(1000); //等会儿，确保服务端启动完成
        
        //::打开客户端会话
        ClientSession clientSession = SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
                .open();
        
        //发送
        clientSession.send("/demo", new StringEntity("hello wrold!"));
    }
}
```

### 2、发送并请求 + 一个答复（像 http）

sendAndRequest + replyEnd（或者 reply。反正只收一个答复，管它是不是最后的）

```java
public class Demo {
    public static void main(String[] args) throws Throwable {
        //::启动服务端
        SocketD.createServer("sd:ws")
                .config(c -> c.port(8602))
                .listen(new SimpleListener(){
                    @Override
                    public void onMessage(Session session, Message message) throws IOException {
                        if(message.isRequest()){
                            session.replyEnd(message, new StringEntity("And you too."));
                        }
                    }
                })
                .start();

        Thread.sleep(1000); //等会儿，确保服务端启动完成
        
        //::打开客户端会话
        ClientSession clientSession = SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
                .open();
        
        //发送并请求（且，等待答复）
        Entity reply = clientSession.sendAndRequest("/demo", new StringEntity("hello wrold!")).await();
    }
}
```

sendAndRequest 的同步模式（一般需要指定超时，默认为 10s）

```java
clientSession.sendAndRequest("/demo", new StringEntity("hello wrold!")).await();
```

sendAndRequest 的异步模式（一般需要指定超时，默认为 10s）

```java
//示例，指定10秒超时
clientSession.sendAndRequest("/demo", new StringEntity("hello wrold!"), 10_000).thenReply(reply->{
    //收到答复
}).thenError(error->{
   //如果有异常（比如超时，或者协议告警）
});
```


### 3、发送并订阅 + 多个答复（像 reactive stream）

sendAndSubscribe + reply(*n) + replyEnd

```java
public class Demo {
    public static void main(String[] args) throws Throwable {
        //::启动服务端
        SocketD.createServer("sd:ws")
                .config(c -> c.port(8602))
                .listen(new SimpleListener(){
                    @Override
                    public void onMessage(Session session, Message message) throws IOException {
                        if(message.isSubscribe()){
                            session.reply(message, new StringEntity("And you too."));
                            session.replyEnd(message, new StringEntity("Welcome to my home"));
                        }
                    }
                })
                .start();

        Thread.sleep(1000); //等会儿，确保服务端启动完成
        
        //::打开客户端会话
        ClientSession clientSession = SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
                .open();
        
        //发送并订阅（且，接收答复流）
        clientSession.sendAndSubscribe("/demo", new StringEntity("hello wrold!")).thenReply(reply->{
            //如果需要识别是否为最后一个答复？
            if(reply.isEnd()){

            }
        }).thenError(error->{
           //如果有异常（比如超时，或者协议告警）
        });
    }
}
```

sendAndSubscribe 只有异步模式（如果有需要，可以指定具体超时。默认为 2h）

```java
//示例，指定60秒超时
clientSession.sendAndSubscribe("/demo", new StringEntity("hello wrold!"), 60_000).thenReply(reply->{
    //如果需要识别是否为最后一个答复？
    if(reply.isEnd()){

    }
}).thenError(error->{
   //如果有异常（比如超时，或者协议告警）
});
```
