### 1、客户端示例代码

使用时，可以根据自己的业务对原生接口包装，进一步简化使用。

```html
<script>
async function demo(){
    //创建单例
    window.clientSession = await SocketD.createClient("sd:ws://127.0.0.1:8602/?u=a&p=2")
            .open();

    //添加用户（加个内容类型，方便与 Mvc 对接）
    const entity = SocketD.newEntity("{id:1,name:'noear'}").metaPut("Content-Type","text/json");
    clientSession.sendAndRequest("/user/add",  entity).thenReply(reply=>{
        const rst = JSON.parse(reply.dataAsString());

        if(rst.code == 200){
            alert("添加成功!");
        }else{
            alert("添加失败");
        }
    })
}

demo();
</script>
```

### 2、服务端示例代码

* 原生接口风格

```java
public class Demo {
    public static void main(String[] args) throws Throwable {
        //创建监听器
        Listener listener =  new EventListener().doOnOpen(s->{
            //鉴权
            if("a".equals(s.param("u")) == false){
                s.close();
            }
        }).doOn("/user/add", (s,m)->{
            if(m.isRequest()){
                s.reply(m, new StringEntity("{\"code\":200}"));
            }
        });
        
        //启动服务
        SocketD.createServer("sd:ws")
                .config(c -> c.port(8602))
                .listen(listener)
                .start();
    }
}
```

* Mvc 接口风格

具体参考 solon 的集成效果：[https://solon.noear.org/article/693](https://solon.noear.org/article/693)

```java
//控制器
@Controller
public class HelloController {
    @Socket
    @Mapping("/hello/add")
    public Result hello(long id, String name) { //{code:200,...}
        return Result.succeed();
    }
}

// event 相当于 http path（注意这个关系）
// data  相当于 http body
// meta  相当于 http header
```