當前位置: 首頁>>代碼示例>>Golang>>正文


Golang Conn.Read方法代碼示例

本文整理匯總了Golang中websocket.Conn.Read方法的典型用法代碼示例。如果您正苦於以下問題:Golang Conn.Read方法的具體用法?Golang Conn.Read怎麽用?Golang Conn.Read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在websocket.Conn的用法示例。


在下文中一共展示了Conn.Read方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: handleWebSocket

func handleWebSocket(conn *websocket.Conn) {
	respStatus := http.StatusOK
	defer func() {
		conn.Close()
		logRequest(HTTPS_WEBSOCKET, respStatus, conn.Request().Host, conn.Request())
	}()
	request := make([]byte, 1024)
	for {
		n, err := conn.Read(request)
		if err != nil {
			if debugMode {
				log.Error("Error reading on WebSocket: %s", err)
			}
			break
		}
		response, status := getLiveItems(string(request[:n]))
		if status != http.StatusOK {
			respStatus = status
			break
		}
		if len(response) != 0 {
			if _, err = conn.Write(response); err != nil {
				break
			}
		}
	}
}
開發者ID:elimisteve,項目名稱:togethr,代碼行數:27,代碼來源:live-server.go

示例2: clientHandler

func clientHandler(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
	}()

	subscriptionChan <- subscription{ws, true}

	for {
		buf := make([]byte, 128)
		n, err := ws.Read(buf)
		if err != nil {
			log.Print("Reading Buffer: ", err)
			break
		}

		var m message
		err = json.Unmarshal(buf[0:n], &m)
		if err != nil {
			log.Print("Parsing JSON: ", buf, m, err)
			break
		}

		messageChan <- message{m.Text, m.Id, m.User, m.Command}
	}
}
開發者ID:matt-west,項目名稱:gochat,代碼行數:26,代碼來源:chat.go

示例3: clientHandler

/*
 * Get messages from client
 */
func clientHandler(ws *websocket.Conn) {
	defer func() {
		log.Println("Client handler closed.")
		ws.Close()
	}()

	buf := make([]byte, 256)
	stopCh := make(chan bool)
	var input ProcessInput
	for {
		n, err := ws.Read(buf)
		if err != nil {
			break
		}

		// get data
		err = json.Unmarshal(buf[0:n], &input)
		if err != nil {
			stopCh <- true
			break
		}

		workChan <- Work{ws, &input, stopCh}
	}
}
開發者ID:egonelbre,項目名稱:sivq,代碼行數:28,代碼來源:server.go

示例4: IsClosed

func IsClosed(ws *websocket.Conn) bool {
	var ibuff [50]byte

	_, err := ws.Read(ibuff[0:50])
	if err != nil {
		return true
	}

	return false
}
開發者ID:hpcorona,項目名稱:webtalk,代碼行數:10,代碼來源:main.go

示例5: ReadThread

func ReadThread(ws *websocket.Conn, msg []byte, ch chan int) {
	for {
		n, error := ws.Read(msg)
		if error != nil {
			fmt.Println("WebSocket read error: ", error)
			ch <- 0
			break
		}
		ch <- n
		if n == 0 {
			break
		}
	}
}
開發者ID:Saikees,項目名稱:practice,代碼行數:14,代碼來源:websocket.go

示例6: sub

func sub(ws *websocket.Conn) {
	println("Beginning Sub")

	var resp = make([]byte, 512)
	for {
		println("Waiting Read")
		_, err := ws.Read(resp)

		if err != nil {
			panic(err)
		}
		// fmt.Println("Received 1:", string(resp[0:n]))
		// fmt.Println("Received %v:", n)
		ch <- 1
	}
}
開發者ID:blindsey,項目名稱:wsbench,代碼行數:16,代碼來源:broadcast_client.go

示例7: clientHandler

func clientHandler(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
	}()

	subscriptionChan <- subscription{ws, true}

	buf := make([]byte, 256)
	for {
		n, err := ws.Read(buf)
		if err != nil {
			break
		}
		messageChan <- buf[0:n]
	}
}
開發者ID:ngd,項目名稱:chrome-bench,代碼行數:17,代碼來源:flicker.go

示例8: indexHandler

func indexHandler(ws *websocket.Conn) {
	var ibuff [50]byte

	i, err := ws.Read(ibuff[0:50])
	if err != nil {
		fmt.Printf("WebSocket error: " + err.String())
		return
	}

	msg := string(ibuff[0:i])
	idx := strings.Index(msg, "!")
	if idx < 0 {
		fmt.Printf("Salt not found\n")
		return
	}

	idstr := msg[0:idx]
	salt := msg[idx+1:]

	uid, err := strconv.Atoui64(idstr)
	if err != nil {
		fmt.Printf("User ID invalid\n")
		return
	}

	defer UnlinkUser(uid, ws)

	usr := LinkUser(uid, salt, ws)
	if usr == nil {
		fmt.Printf("Cannot link with User ID\n")
		return
	}

	for usr.exit == false {
		time.Sleep(1e8)

		if IsClosed(ws) {
			break
		}
	}
}
開發者ID:hpcorona,項目名稱:webtalk,代碼行數:41,代碼來源:main.go

示例9: doEventStream

func doEventStream(ws *websocket.Conn) {
	ID := chat.UUID()
	defer func() {
		subscriptionChan <- &Subscription{ws, chat.REMOVE_CONNECTION, &User{ID, nil, ""}}
		ws.Close()
	}()

	subscriptionChan <- &Subscription{ws, chat.ADD_CONNECTION, &User{ID, nil, ""}}
	newStartMessage, _ := json.Marshal(
		MessageData{
			"start",
			map[string]interface{}{
				"ID": ID,
			}})

	ws.Write(newStartMessage)

	for {
		buf := make([]byte, 1024)

		n, err := ws.Read(buf)
		if err != nil {
			log.Println("Error reading from websocket connection: ", err.Error())
			break
		}

		newMessageData := new(MessageData)
		err = json.Unmarshal(buf[0:n], newMessageData)

		if err != nil {
			log.Println("Error unmarshaling message: ", string(buf[0:n]), " : ", err.Error())
		}

		messageChan <- message{
			ws,
			*newMessageData,
		}
	}
}
開發者ID:natelong,項目名稱:webchat,代碼行數:39,代碼來源:webchat.go

示例10: broadcastHandler

func broadcastHandler(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
		fmt.Printf("Closed \n")
	}()

	fmt.Printf("Adding to subscription \n")

	subscriptionChan <- subscription{ws, true}
	fmt.Printf("Added to subscription \n")

	buf := make([]byte, 256)
	for {
		n, err := ws.Read(buf)
		fmt.Printf("Reding message %v \n", n)

		if err != nil {
			break
		}
		messageChan <- buf[0:n]
	}
}
開發者ID:blindsey,項目名稱:wsbench,代碼行數:23,代碼來源:wsbench_test.go

示例11: doEventStream

func doEventStream(ws *websocket.Conn) {
	defer func() {
		subscriptionChan <- subscription{ws, false}
		ws.Close()
	}()

	subscriptionChan <- subscription{ws, true}

	for {
		buf := make([]byte, 512)

		n, err := ws.Read(buf)
		if err != nil {
			log.Println("Error reading from websocket connection")
			break
		}

		messageChan <- message{
			ws,
			buf[0:n],
		}
	}
}
開發者ID:natelong,項目名稱:playlistr,代碼行數:23,代碼來源:playlistr.go

示例12: wssServe

func wssServe(ws *websocket.Conn) {
	fmt.Println("Socket connect")
	ws.Write([]byte(strconv.Itoa(int(*state))))
	b := make([]byte, 2)
	var quit bool
	for !quit {
		ws.Read(b)
		t, _ := strconv.Atoi(string(b))
		fmt.Println(t)
		switch t {
		case 0:
			func() {
				fmt.Println("Server Init")
				if *state > 0 {
					ws.Write([]byte(strconv.Itoa(int(*state))))
				} else {
					ws.Write([]byte("0"))
					b = make([]byte, 5)
					ws.Read(b)
					world, _ := strconv.Atob(string(b))
					fmt.Println(world)
					if world {

					} else {

					}
				}
			}()
		default:
			func() {
				fmt.Println(string(b))
				quit = true
			}()
		}
		quit = true
	}
}
開發者ID:CasualSuperman,項目名稱:Ourcraft,代碼行數:37,代碼來源:gui.go


注:本文中的websocket.Conn.Read方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。