func main() {
go func() {
err := http.ListenAndServe(":9998", websocket.Server{websocket.Config{},
nil, ChatroomServer})
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}()
err := http.ListenAndServe(":9999", nil)
if err != nil {
panic("ListenAndServe: " + err.Error())
}
}
func ChatroomServer(ws *websocket.Conn) {
defer func(){
ws.Close()
}()
var msgReceive string
var msgToSend string
for{
if err := websocket.Message.Receive(ws,&msgReceive);err!=nil{
fmt.Println("Can't receive")
}
if err := websocket.Message.Send(ws,&msgToSend);err!=nil{
fmt.Println("Can't send")
}
}
}
直接使用http.handler("/", websocket.Handler(ChatroomServer))
这种方式会报错.
目前猜测应该是websocket不能和其它的玩意共享一个端口,必须是独立使用一个端口..仅为个人猜测
网友评论