一个频道只能接收一条消息。

huangapple go评论84阅读模式
英文:

can only receive one message on a channel

问题

我已经创建了一个全局通道,并在ExampleNewWatcher内部的每次新事件发生时向其发送消息。第一次发生事件时,消息会在通道的接收端接收到。然而,第二次发生事件时,我在通道的接收端没有收到消息。

var messages = make(chan string)
func ExampleNewWatcher(){
  watcher, err := fsnotify.NewWatcher()
  if err != nil{
    log.Fatal(err)
  }
  defer watcher.Close()
  done := make(chan bool)
  go func(){
     for {
        select{
           case event := <- watcher.Events:
              if event.Op&fsnotify.Write == fsnotify.Write{
                 log.Println("如果我创建了两个写事件,这个日志会打印两次")
                 messages <- "即使我创建了两个事件,这条消息只会发送一次"
              }
           case err := <- watcher.Errors:
               log.Println("错误", err)
        }
     
     }
  }()
  err = watcher.Add("/tmp/watchedfile")
  <-done
}
func Websock(ws *websocket.Conn){
   msg := <-messages
   log.Println("即使我创建了两个事件,只有一条消息到达", msg)
   err := websocket.Message.Send(ws, msg)
  
}
func main(){
 go ExampleNewWatcher() //由于某种原因,除非我在这个观察者函数前加上一个go例程,否则服务器不会运行,但这是另一个问题...

  http.Handle("/websock", websocket.Handler(Websock))
 log.Fatal(http.ListenAndServe(":8000", nil))

}
英文:

I've created a global channel, and am sending messages to it whenever a new event occurs inside of ExampleNewWatcher. The first time an event occurs, the message is getting received in the receiving end of the channel. However, the second time an event occurs, I am not receiving a message in the receiving end of the channel.

var messages = make(chan string)
func ExampleNewWatcher(){
  watcher, err := fsnotify.NewWatcher()
  if err != nil{
    log.Fatal(err)
  }
  defer watcher.Close(0
  done := make(chan bool)
  go func(){
     for {
        select{
           case event := &lt;- watcher.Events:
              if event.Op&amp;fsnotify.Write == fsnotify.Write{
                 log.Println(&quot;this logs twice if I create two write events&quot;)
                 messages &lt;- &quot;this message only gets sent once even if I create two events&quot;
              }
           case err := &lt;- watcher.Errors:
               log.Println(&quot;error&quot;, err)
        }
     
     }
  }()
  err = watcher.Add(&quot;/tmp/watchedfile&quot;)
  &lt;-done
}
func Websock(ws *websocket.Conn){
   msg := &lt;-messages
   log.Println(&quot;only one message arrives, even if I create for two events&quot;, msg)
   err := websocket.Message.Send(ws, msg)
  
}
func main(){
 go ExampleNewWatcher() //for some reason, the server doesn&#39;t run unless I prefix a go routine to this watcher function, but that&#39;s another issue ...

  http.Handle(&quot;/websock&quot;, websocket.Handler(Websock))
 log.Fatal(http.ListenAndServe(&quot;:8000&quot;, nil))

}

答案1

得分: 2

您的WebSock函数需要包含一个接收循环以接收多个消息。否则,您的函数将终止并且WebSocket连接将被关闭。

英文:

Your WebSock function need to include a receive loop to receive more than one message. Otherwise, your function will terminate and the websocket connection will be closed.

答案2

得分: 0

@Mickaël Rémont是正确的。将你的Websock函数修改为以下形式:

func Websock(ws *websocket.Conn) {
    for {
        msg := <-messages
        log.Println("只有一个消息到达,即使我为两个事件创建了消息", msg)
        if err := websocket.Message.Send(ws, msg); err != nil {
            log.Fatal(err)
        }
    }
}

请注意,这是一个Go语言的代码示例,用于处理WebSocket连接。它的作用是从messages通道中接收消息,并将其发送给WebSocket连接。如果发送消息时出现错误,将记录错误并终止程序。

英文:

@Mickaël Rémond is right. Modify your Websock function to something like:

func Websock(ws *websocket.Conn) {
	for {
		msg := &lt;-messages
		log.Println(&quot;only one message arrives, even if I create for two events&quot;, msg)
		if err := websocket.Message.Send(ws, msg); err != nil {
			log.Fatal(err)
		}
	}
}

huangapple
  • 本文由 发表于 2015年12月27日 20:28:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/34480387.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定