繁花如伊
没关系,我在网上找到了一些帮助代码。现在我的代码看起来像这样,以防其他人需要这个:package mainimport ( "net/http" "text/template" "code.google.com/p/go.net/websocket" "fmt" "os" "time")const address string = "localhost:9999"func main() { initWebsocketClient()}func initWebsocketClient() { fmt.Println("Starting Client") ws, err := websocket.Dial(fmt.Sprintf("ws://%s/ws", address), "", fmt.Sprintf("http://%s/", address)) if err != nil { fmt.Printf("Dial failed: %s\n", err.Error()) os.Exit(1) } incomingMessages := make(chan string) go readClientMessages(ws, incomingMessages) i := 0 for { select { case <-time.After(time.Duration(2e9)): i++ response := new(Message) response.RequestID = i response.Command = "Eject the hot dog." err = websocket.JSON.Send(ws, response) if err != nil { fmt.Printf("Send failed: %s\n", err.Error()) os.Exit(1) } case message := <-incomingMessages: fmt.Println(`Message Received:`,message) } }}func readClientMessages(ws *websocket.Conn, incomingMessages chan string) { for { var message string // err := websocket.JSON.Receive(ws, &message) err := websocket.Message.Receive(ws, &message) if err != nil { fmt.Printf("Error::: %s\n", err.Error()) return } incomingMessages <- message }}同样正如recoba在评论中所建议的那样,这里有一个新的大猩猩示例,供寻求更好解决方案的人使用。