htmlwebsocket

在服务器上,我使用"code.google.com/p/go.net/websocket"中的示例只接受连接.

但是,在Chrome26中,响应是

与'ws://127.0.0.1:1234 /'的WebSocket连接失败:意外的响应代码:400

是否遗漏了某些东西(如握手)?

htmlgo

WebSocket Echo Test




// ------------------------------

package main

import (
    "code.google.com/p/go.net/websocket"
    "fmt"
    "log"
    "net/http"
)

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)

        if err = websocket.Message.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}

func main() {
    http.Handle("/", websocket.Handler(Echo))
    if err := http.ListenAndServe(":1234", nil); err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}

Chris Farmil.. 5

Chrome可能会抛出400错误,因为它认为您正在尝试向websocket服务器执行跨域请求,并认为您不太可能获得权限.

go
sock.go
package main

import (
    "code.google.com/p/go.net/websocket"
    "fmt"
    "log"
    "net/http"
)

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)

        if err = websocket.Message.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}

func main() {
    http.Handle("/", http.FileServer(http.Dir("."))) // <-- note this line
    http.Handle("/socket", websocket.Handler(Echo))
    log.Println("serving")
    if err := http.ListenAndServe(":1234", nil); err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}
index.html

WebSocket Echo Test




现在,您将能够在chrome中进行连接.



1> Chris Farmil..:

Chrome可能会抛出400错误,因为它认为您正在尝试向websocket服务器执行跨域请求,并认为您不太可能获得权限.

go
sock.go
package main

import (
    "code.google.com/p/go.net/websocket"
    "fmt"
    "log"
    "net/http"
)

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)

        if err = websocket.Message.Send(ws, msg); err != nil {
            fmt.Println("Can't send")
            break
        }
    }
}

func main() {
    http.Handle("/", http.FileServer(http.Dir("."))) // <-- note this line
    http.Handle("/socket", websocket.Handler(Echo))
    log.Println("serving")
    if err := http.ListenAndServe(":1234", nil); err != nil {
        log.Fatal("ListenAndServe:", err)
    }
}
index.html

WebSocket Echo Test




现在,您将能够在chrome中进行连接.