package main
import (
"fmt"
"log"
"net/http"
"github.com/soekchl/websocket"
)
func main() {
fmt.Println("Start app")
http.Handle("/", websocket.Handler(Echo))
if err := http.ListenAndServe(":7070", nil); err != nil {
log.Fatal("ListenAndServe:", err)
}
}
func Echo(ws *websocket.Conn) {
var err error
for {
var reply string
if err = websocket.Message.Receive(ws, &reply); err != nil {
fmt.Println("Can't receive")
break
}
fmt.Println("Received back from client: " + reply)
msg := "Received: " + reply
fmt.Println("Sending to client: " + msg + "_server")
if err = websocket.Message.Send(ws, msg+"_server"); err != nil {
fmt.Println("Can't send")
break
}
}
}
golang websocket 服务器
最新推荐文章于 2024-08-09 20:43:09 发布