49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package wsdemo
|
|
|
|
import (
|
|
"ciyon/web/admin"
|
|
c "ciyon/zciyon"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
func Wsdemo(w http.ResponseWriter, r *http.Request) bool {
|
|
ws, err := c.NewCiyWebsocket(w, r)
|
|
if err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return false
|
|
}
|
|
post := c.NewCiyPost(w, r)
|
|
_, userid := admin.Verifyfast(r, c.CiyDB, post)
|
|
if userid == 0 {
|
|
ws.SendFail(0, "请重新登录")
|
|
ws.Close()
|
|
return false
|
|
}
|
|
fmt.Println(userid)
|
|
//defer ws.Close()
|
|
ws.OnMessage(func(byt []byte) {
|
|
fmt.Println("Received message:", len(byt))
|
|
})
|
|
ws.OnMessageJSON(func(code, wsidx int, json map[string]any) {
|
|
fmt.Println("Received json:", c.Tostr(code), "_wsid:", c.Tostr(wsidx))
|
|
ws.SendFail(wsidx, "err:Sdfasdf")
|
|
})
|
|
ws.OnError(func(error) {
|
|
fmt.Println("Websocket error:", err)
|
|
})
|
|
ws.OnClose(func() {
|
|
fmt.Println("Websocket closed")
|
|
})
|
|
for {
|
|
// ws.SendSucc(4)
|
|
// ws.SendFail(2, "err:Sdfasdf")
|
|
ws.Send([]byte("heasdfhaeytwehjgdjnsfllo"))
|
|
if ws.Isclose {
|
|
break
|
|
}
|
|
c.Sleep(5)
|
|
}
|
|
return true
|
|
}
|