dpsx99068 2017-10-11 19:50
浏览 62
已采纳

使用websockets Golang将更改推送到客户端

I have been trying to push changes done on server side using websockets in Golang.

I have achived what i wanted by this code

Server side code:

package main

import (
   "golang.org/x/net/websocket"
   "fmt"
   "net/http"
   "os"
   "bufio"
)

type chat struct {
    message string
}

func (ms *chat) Echo(ws *websocket.Conn) {
    var reply string
    for {
         if reply !=  ms.message {
         websocket.Message.Send(ws, ms.message)
         }
         websocket.Message.Receive(ws, reply)
    }
}

func NewMessage(s *string){
   reader := bufio.NewReader(os.Stdin)
   for {
       fmt.Print("Enter text: ")
       *s, _ = reader.ReadString('
')
       }
   }

func main() {
   myString := chat{"Welcome"}
   go NewMessage(&myString.message)
   http.Handle("/", websocket.Handler(myString.Echo))
   http.ListenAndServe(":1234", nil)
}

Client side code:

<html>
<body>
<script type="text/javascript">
    var exampleSocket = new WebSocket("ws://127.0.0.1:1234");

    exampleSocket.onopen = function (event) {
    console.log("connected");
    };

    exampleSocket.onmessage = function (event) {
        console.log(event.data);
        exampleSocket.send(event.data)
        document.getElementById("p1").innerHTML = event.data;
    }


</script>
<h1>WebSockets</h1>
<form>
    <p id="p1" value="Hello, world!"/>
</form>

The problem with above code is repeatedly sending ms.message as if reply != ms.message does not work. Simply var reply string value is not being changed by websocket.Message.Receive(ws, reply).

I have tried passing reply as a pointer but it does not update value on the client side dynamically.

Here is how server side would look like after the change:

package main

import (
   "golang.org/x/net/websocket"
   "fmt"
   "net/http"
   "os"
   "bufio"
)

type chat struct {
    message string
}

func (ms *chat) Echo(ws *websocket.Conn) {
    var reply string
    for {
         if reply !=  ms.message {
         websocket.Message.Send(ws, ms.message)
         }
         websocket.Message.Receive(ws, &reply)
    }
}

func NewMessage(s *string){
   reader := bufio.NewReader(os.Stdin)
   for {
       fmt.Print("Enter text: ")
       *s, _ = reader.ReadString('
')
       }
   }

func main() {
   myString := chat{"Welcome"}
   go NewMessage(&myString.message)
   http.Handle("/", websocket.Handler(myString.Echo))
   http.ListenAndServe(":1234", nil)
}

This code does not fit my expectations.

Is it possible to dynamically update string on clients side without sending value multiple times as it is being done in the example number 1?

  • 写回答

1条回答 默认 最新

  • dongtui4038 2017-10-12 08:25
    关注

    Simply putting websocket.Message.Receive in the if statement fixes the problem.

    package main
    
    import (
       "golang.org/x/net/websocket"
       "fmt"
       "net/http"
       "os"
       "bufio"
    )
    
    type chat struct {
       message string
    }
    
    func (ms *chat) Echo(ws *websocket.Conn) {
       var reply string
          for {
             if reply !=  ms.message {
                websocket.Message.Send(ws, ms.message)
                websocket.Message.Receive(ws, &reply)
             }
    
          }
    }
    
    func NewMessage(s *string){
    reader := bufio.NewReader(os.Stdin)
    for {
       fmt.Print("Enter text: ")
       *s, _ = reader.ReadString('
    ')
      }
    }
    
    func main() {
       myString := chat{"Welcome"}
       go NewMessage(&myString.message)
       http.Handle("/", websocket.Handler(myString.Echo))
       http.ListenAndServe(":1234", nil)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器