英文:
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 := <- watcher.Events:
if event.Op&fsnotify.Write == fsnotify.Write{
log.Println("this logs twice if I create two write events")
messages <- "this message only gets sent once even if I create two events"
}
case err := <- watcher.Errors:
log.Println("error", err)
}
}
}()
err = watcher.Add("/tmp/watchedfile")
<-done
}
func Websock(ws *websocket.Conn){
msg := <-messages
log.Println("only one message arrives, even if I create for two events", msg)
err := websocket.Message.Send(ws, msg)
}
func main(){
go ExampleNewWatcher() //for some reason, the server doesn't run unless I prefix a go routine to this watcher function, but that's another issue ...
http.Handle("/websock", websocket.Handler(Websock))
log.Fatal(http.ListenAndServe(":8000", 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 := <-messages
log.Println("only one message arrives, even if I create for two events", msg)
if err := websocket.Message.Send(ws, msg); err != nil {
log.Fatal(err)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论