本文整理汇总了Golang中github.com/desertbit/glue.Socket.OnRead方法的典型用法代码示例。如果您正苦于以下问题:Golang Socket.OnRead方法的具体用法?Golang Socket.OnRead怎么用?Golang Socket.OnRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/desertbit/glue.Socket
的用法示例。
在下文中一共展示了Socket.OnRead方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HandleSocket
func HandleSocket(s *glue.Socket) {
s.OnClose(func() {
err := UnsubscribeAll(s)
if err != nil {
log.Print(err)
}
})
s.OnRead(func(data string) {
fields := strings.Fields(data)
if len(fields) == 0 {
return
}
switch fields[0] {
case "watch":
if len(fields) != 2 {
return
}
err := Subscribe(s, fields[1])
if err != nil {
log.Print(err)
}
case "touch":
if len(fields) != 4 {
return
}
err := Emit(fields[1], "touch:"+fields[2]+","+fields[3])
if err != nil {
log.Print(err)
}
}
})
}
示例2: HandleSocket
func (h *Hub) HandleSocket(sock *glue.Socket) {
sock.OnClose(func() {
h.nexus.detach(sock)
})
sock.OnRead(func(data string) {
fields := strings.SplitN(data, " ", 2)
if len(fields) != 2 {
return
}
switch fields[0] {
case "attach":
h.attachInCh <- AttachIn{
Socket: sock,
Token: fields[1],
}
case "change":
data := ChangeData{}
err := json.Unmarshal([]byte(fields[1]), &data)
if err != nil {
h.sendError(sock, "bad request")
return
}
h.changeInCh <- ChangeIn{
Socket: sock,
Change: data.Change(),
}
}
})
}
示例3: onNewSocket
func onNewSocket(s *glue.Socket) {
s.OnRead(func(data string) {
// Echo back.
s.Write(data)
})
s.OnClose(func() {
println("closed")
})
s.Write("Hello Client")
}
示例4: onNewSocket
func onNewSocket(s *glue.Socket) {
// Set a function which is triggered as soon as the socket is closed.
s.OnClose(func() {
log.Printf("socket closed with remote address: %s", s.RemoteAddr())
})
// Set a function which is triggered during each received message.
s.OnRead(func(data string) {
// Echo the received data back to the client.
s.Write(data)
})
// Send a welcome string to the client.
s.Write("Hello Client")
}