本文整理匯總了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
}
}
}
}
示例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}
}
}
示例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}
}
}
示例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
}
示例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
}
}
}
示例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
}
}
示例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]
}
}
示例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
}
}
}
示例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,
}
}
}
示例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]
}
}
示例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],
}
}
}
示例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
}
}