本文整理汇总了Golang中golang.org/x/net/websocket.Conn.Read方法的典型用法代码示例。如果您正苦于以下问题:Golang Conn.Read方法的具体用法?Golang Conn.Read怎么用?Golang Conn.Read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/net/websocket.Conn
的用法示例。
在下文中一共展示了Conn.Read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: WebsocketHandler
func WebsocketHandler(ws *websocket.Conn) {
key := ws.Request().Header.Get("Sec-Websocket-Key")
client := Client{ws, key}
Srv.register(client)
defer func() {
Srv.unregister(client)
}()
for {
msg := make([]byte, 512)
n, err := ws.Read(msg)
if err != nil {
break
}
var cmd Command
json.Unmarshal(msg[:n], &cmd)
log.Println(cmd.Event)
client.process(cmd)
}
}
示例2: protocol
func protocol(conn *websocket.Conn) (*Request, error) {
// read content length
buf := make([]byte, 8)
n, err := conn.Read(buf)
if err != nil {
return nil, err
}
if n != 8 {
return nil, ErrPackageHeaderLength
}
length, err := strconv.ParseInt(string(buf), 10, 32)
if err != nil {
return nil, ErrPackageHeaderLength
}
// limit to 50M
if length < 1 || length > 50*1024*1024 {
return nil, ErrContentLength
}
// read all content by length
buffer := new(bytes.Buffer)
i, err := io.CopyN(buffer, conn, length)
if i != length {
return nil, ErrPackageHeaderLength
}
// parse request
return ParseRequest(buffer.Bytes())
}
示例3: handleTrafficWS
func handleTrafficWS(conn *websocket.Conn) {
trafficMutex.Lock()
for _, traf := range traffic {
if !traf.Position_valid { // Don't send unless a valid position exists.
continue
}
trafficJSON, _ := json.Marshal(&traf)
conn.Write(trafficJSON)
}
trafficMutex.Unlock()
// Subscribe the socket to receive updates.
trafficUpdate.AddSocket(conn)
// Connection closes when function returns. Since uibroadcast is writing and we don't need to read anything (for now), just keep it busy.
for {
buf := make([]byte, 1024)
_, err := conn.Read(buf)
if err != nil {
break
}
if buf[0] != 0 { // Dummy.
continue
}
time.Sleep(1 * time.Second)
}
}
示例4: socketHandler
func socketHandler(ws *websocket.Conn) {
for {
receivedStream := make([]byte, 256)
byteCount, err := ws.Read(receivedStream)
if err != nil {
return
}
if byteCount > 0 {
inputHeaders := make(map[string]string)
decode_err := json.Unmarshal(receivedStream[:byteCount], &inputHeaders)
if decode_err != nil {
return
}
out := runPlugins("secred_key", "endpoint_XXX", "consumer_XXX")
jsonOut, encode_err := json.Marshal(out.Headers)
if encode_err != nil {
return
}
ws.Write(jsonOut)
}
}
}
示例5: testServerMetrics
func testServerMetrics(ws *websocket.Conn) {
var b []byte
b = make([]byte, 1000)
length, _ := ws.Read(b)
var m metricsPayload
_ = json.Unmarshal(b[:length], &m)
test := mtests[currentTest]
if m.User != test.expMetrics.User {
globalT.Fatalf("Unexpected user")
}
for _, v := range m.Metrics.Tasks {
appFound := false
for _, vv := range test.expMetrics.Metrics.Tasks {
if vv.App == v.App {
appFound = true
if v.PendingCount != vv.PendingCount || v.RunningCount != vv.RunningCount {
log.Debugf("%#v", test.expMetrics.Metrics.Tasks)
log.Debugf("%#v", m.Metrics.Tasks)
globalT.Fatalf("Unexpected values")
}
}
}
if !appFound {
globalT.Fatalf("Received unexpected metric for %s", v.App)
}
}
}
示例6: StartReading
func StartReading(conn *websocket.Conn) (<-chan []byte, <-chan bool) {
log.Print("start reading...")
var msg []byte
sendChan := make(chan []byte)
breakChan := make(chan bool)
go func() {
for {
var tmp = make([]byte, 512)
n, err := conn.Read(tmp)
if err == io.EOF {
breakChan <- true
break
}
if err != nil {
log.Fatal(err)
}
if msg != nil {
msg = append(msg, tmp[:n]...)
} else {
msg = tmp[:n]
}
if n != 512 {
sendChan <- msg
msg = nil
}
}
}()
return sendChan, breakChan
}
示例7: websocketUploadServer
func websocketUploadServer(ws *websocket.Conn) {
dlId := ws.Request().URL.Path[len("/api/upload_ws/"):]
bf := GetBounceFile(dlId)
if bf == nil {
ws.Write([]byte("ERROR! Download with this ID does not exist."))
return
}
var dataBuffer [50000]byte // 50 KB arbitrary
total := 0
for {
numBytes, err := ws.Read(dataBuffer[:])
total += numBytes
dataCopy := append([]byte{}, dataBuffer[:numBytes]...)
bf.SendData(dataCopy)
if err == io.EOF {
break
}
if err != nil { // Oops!
ws.Write([]byte("ERROR! " + err.Error()))
break
}
ws.Write([]byte("OK"))
}
}
示例8: handler
// Main handler upon client web connection to the server
func handler(sock *websocket.Conn) {
fmt.Println("- - - - - - - - - - - - - - - - - -")
fmt.Println("Client has connected to the server!")
// Read the initial message upon client connection
var msg = make([]byte, 512)
_, err := sock.Read(msg)
if err != nil {
log.Println(err)
return
}
for {
// Receive the sftp auth information and store in a map
n, err := sock.Read(msg)
if err != nil {
log.Println(err)
return
}
fmt.Println("Received data from the client: ", string(msg[:n]))
json := parse(msg[:n])
go handle[json.Function](json.ConnId, json.Data, sock)
}
}
示例9: readConn
func readConn(t *testing.T, ws *websocket.Conn) {
buf := make([]byte, 4096)
n, err := ws.Read(buf)
if err != nil {
t.Error(err)
}
t.Log(string(buf[:n]))
}
示例10: read
func read(t *testing.T, conn *websocket.Conn) string {
var data = make([]byte, 512)
n, err := conn.Read(data)
if err != nil {
t.Errorf("conn.Read(%v) got error: %v, want nil", data, err)
}
return string(data[0:n])
}
示例11: EchoServer
func EchoServer(ws *websocket.Conn) {
cm.register <- ws
for {
buf := make([]byte, 256)
ws.Read(buf)
cm.broadcast <- buf
}
}
示例12: readKey
func readKey(ws *websocket.Conn) string {
var key = make([]byte, KEY_LENGTH+4) // KEY_LENGTH + 4 because what we receive is ["..key."]
_, err := ws.Read(key)
if err != nil {
//log.Println(err)
return ""
}
return cleanKey(key)
}
示例13: WSRead
// WSRead reads from a websocket and print the read messages to STDOUT.
func WSRead(ws *websocket.Conn) {
msg := make([]byte, 512)
for {
n, err := ws.Read(msg)
if err != nil {
log.Fatal(err)
}
fmt.Printf("<< %s\n", msg[:n])
}
}
示例14: WebsocketHandler
func WebsocketHandler(ws *websocket.Conn) {
id := eventManager.On("*@websocket", func(topic string, payload interface{}) {
ws.Write([]byte(topic))
})
data := make([]byte, 1)
ws.Read(data)
// received bytes or connection died: close it.
eventManager.Off(id)
ws.Close()
}
示例15: webHandler
func webHandler(conn *websocket.Conn) {
data := make([]byte, 120)
n, err := conn.Read(data)
checkerr(err)
fmt.Printf("msg<------[%s]\n", string(data[:n]))
m, err := conn.Write(data[:n])
checkerr(err)
fmt.Printf("msg------->[%s]\n", string(data[:m]))
defer conn.Close()
}