Socket.D v2.5.11

会话

</> markdown

提醒下,这个没法自己实现。如果你想增加方法,需要参与开源贡献:)

1、会话

  • Session 会话关键接口(可双向互发互答)
接口描述
session::send(event, entity)->SendStream发送(Qos0)
session::sendAndRequest(event, entity)->RequestStream发送并请求(且,只收一个答复。Qos1)
//要求必须答复一次
session::sendAndRequest(event, entity, timeout)->RequestStream带超时
session::sendAndSubscribe(event, entity)->SubscribeStream发送并订阅(且,接收多个答复。Streams)
//在答复结束之前,不限答复次数。
session::sendAndSubscribe(event, entity, timeout)->SubscribeStream带超时
session::reply(from, entity)答复
session::replyEnd(from, entity)答复结束
session::colse()关闭(“本端”不能再发消息,心跳中止)

2、会话接口

  • Session 完整接口(以 Java 为例)
/**
 * 会话
 */
public interface Session extends ClientSession, Closeable {
    /**
     * 获取远程地址
     */
    InetSocketAddress remoteAddress() throws IOException;

    /**
     * 获取本地地址
     */
    InetSocketAddress localAddress() throws IOException;

    /**
     * 获取握手信息
     */
    Handshake handshake();

    /**
     * broker player name
     *
     * @since 2.1
     */
    default String name() {
        return param("@");
    }

    /**
     * 获取握手参数
     *
     * @param name 名字
     */
    String param(String name);

    /**
     * 获取握手参数或默认值
     *
     * @param name 名字
     * @param def  默认值
     */
    String paramOrDefault(String name, String def);

    /**
     * 获取握手路径
     */
    String path();

    /**
     * 设置握手新路径
     */
    void pathNew(String pathNew);

    /**
     * 获取所有属性
     */
    Map<String, Object> attrMap();

    /**
     * 是有属性
     *
     * @param name 名字
     */
    boolean attrHas(String name);

    /**
     * 获取属性
     *
     * @param name 名字
     */
    <T> T attr(String name);

    /**
     * 获取属性或默认值
     *
     * @param name 名字
     * @param def  默认值
     */
    <T> T attrOrDefault(String name, T def);

    /**
     * 放置属性
     *
     * @param name  名字
     * @param value 值
     */
    <T> Session attrPut(String name, T value);

    /**
     * 是否有效
     */
    boolean isValid();
    
    /**
     * 是否关闭中
     * */
    boolean isClosing();

    /**
     * 获取会话Id
     */
    String sessionId();

    /**
     * 手动重连(一般是自动)
     */
    void reconnect() throws IOException;

    /**
     * 手动发送 Ping(一般是自动)
     */
    void sendPing() throws IOException;

    /**
     * 发送告警
     */
    void sendAlarm(Message from, String alarm) throws IOException;

    /**
     * 发送
     *
     * @param event  事件
     * @param entity 实体
     * @return 流
     */
    SendStream send(String event, Entity entity) throws IOException;

    /**
     * 发送并请求
     *
     * @param event  事件
     * @param entity 实体
     * @return 流
     */
    default RequestStream sendAndRequest(String event, Entity entity) throws IOException {
        return sendAndRequest(event, entity, 0L);
    }

    /**
     * 发送并请求
     *
     * @param event   事件
     * @param entity  实体
     * @param timeout 超时(单位:毫秒)
     * @return 流
     */
    RequestStream sendAndRequest(String event, Entity entity, long timeout) throws IOException;


    /**
     * 发送并订阅(答复结束之前,不限答复次数)
     *
     * @param event  事件
     * @param entity 实体
     * @return 流
     */
    default SubscribeStream sendAndSubscribe(String event, Entity entity) throws IOException {
        return sendAndSubscribe(event, entity, 0L);
    }

    /**
     * 发送并订阅(答复结束之前,不限答复次数)
     *
     * @param event   事件
     * @param entity  实体
     * @param timeout 超时(单位:毫秒)
     * @return 流
     */
    SubscribeStream sendAndSubscribe(String event, Entity entity, long timeout) throws IOException;

    /**
     * 答复
     *
     * @param from    来源消息
     * @param content 内容
     */
    void reply(Message from, Entity content) throws IOException;

    /**
     * 答复并结束(即最后一次答复)
     *
     * @param from    来源消息
     * @param content 内容
     */
    void replyEnd(Message from, Entity content) throws IOException;
    
    /**
     * 预关闭(用于两段式关闭,执行后对端的 session.isClosing() 为 true)
     */
    void preclose() throws IOException;
    
    /**
     * 关闭
     */
    void close() throws IOException;
}