go-iris-websocket 简单聊天通信

本文介绍如何使用 Iris 框架的 WebSocket 封装进行实时通信。通过自定义消息格式和 OnNativeMessage 事件,实现了客户端与服务器之间的简单聊天功能。代码示例包括 Go 服务端设置和 HTML 客户端交互。

基于go的websocket大多使用gorilla/websocket

iris也提供了websoket的封装,github.com/kataras/iris/v12/websocket

不过iris官方给的示例基本上都是依赖官方的js库实现的neffos.js

Neffos.js对websocket进行了封装,主要是房间进入和离开等事件的绑定,

对于消息的传递也使用了自己定义的格式,不同的字段使用分号进行分割

例如:;;;;0;0;helloworld,查看iris中源码看到Message的定义:


type Message struct {
   
   
wait string
// The Namespace that this message sent to/received from.
Namespace string
// The Room that this message sent to/received from.
Room string
// The Event that this message sent to/received from.
Event string
// The actual body of the incoming/outcoming data.
Body []byte
// The Err contains any message's error, if any.
// Note that server-side and client-side connections can return an error instead of a message from each event callbacks,
// except the clients's force Disconnect which its local event doesn't matter when disconnected manually.
Err error
// if true then `Err` is filled by the error message and
// the last segment of incoming/outcoming serialized message is the error message instead of the body.
isError bool
isNoOp  bool
isInvalid bool
// the CONN ID, filled automatically if `Server#Broadcast` first parameter of sender connection's ID is not empty,
// not exposed to the subscribers (rest of the clients).
// This is the ID across neffos servers when scale.
from string
// When sent by the same connection of the current running server instance.
// This field is serialized/deserialized but it's clean on sending or receiving from a client
// and it's only used on StackExchange feature.
// It's serialized as the first parameter, instead of wait signal, if incoming starts with 0x.
FromExplicit string // the exact Conn's pointer in this server instance.
// Reports whether this message is coming from a stackexchange.
// This field is not exposed and it's not serialized at all, ~local-use only~.
//
// The "wait" field can determinate if this message is coming from a stackexchange using its second char,
// This value set based on "wait" on deserialization when coming from remote side.
// Only server-side can actually set it.
FromStackExchange bool
// To is the connection ID of the receiver, used only when `Server#Broadcast` is called, indeed when we only need to send a message to a single connection.
// The Namespace, Room are still respected at all.
//
// However, sending messages to a group of connections is done by the `Room` field for groups inside a namespace or just `Namespace` field as usual.
// This field is not filled on sending/receiving.
To string
// True when event came from local (i.e client if running client) on force disconnection,
// i.e OnNamespaceDisconnect and OnRoomLeave when closing a conn.
// This field is not filled on sending/receiving.
// Err does not matter and never sent to the other side.
IsForced bool
// True when asking the other side and fire the respond's event (which matches the sent for connect/disconnect/join/leave),
// i.e if a client (or server) onnection want to connect
// to a namespace or join to a room.
// Should be used rarely, state can be checked by `Conn#IsClient() bool`.
// This field is not filled on sending/receiving.
IsLocal bool
// True when user define it for writing, only its body is written as raw native websocket message, namespace, event and all other fields are empty.
// The receiver should accept it on the `OnNativeMessage` event.
// This field is not filled on sending/receiving.
IsNative bool
// Useful rarely internally on `Conn#Write` namespace and rooms checks, i.e `Conn#DisconnectAll` and `NSConn#RemoveAll`.
// If true then the writer's checks will not lock connectedNamespacesMutex or roomsMutex again. May be useful in the future, keep that solution.
locked bool
// if server or client should write using Binary message or if the incoming message was readen as binary.
SetBinary bool
}

注意到有个IsNative的控制参数,使用OnNativeMessage事件和IsNative字段就可以使用自己的格式通过websoket传递数据:

<
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ango_Cango

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值