2013-11-06 00:51
浏览 68


I am trying to implement a global button counter that updates as any/different users click it. So the idea is if one person clicks the button, I see the counter update on my instance of the page.

I currently have the long polling technique working, or so I think, but after review I believe I have an error with "broadcasting" the update to all browsers.

The error currently is that if for example I have two browsers open, and I continuously click on one browser, that browser that I click the button only updates half the time. It will get 1 3 5 etc while the other browser displays 2 4 6 etc.

After reviewing online, I think this may have to do with channels and broadcasting to all those browsers that are on the site. If anyone can help me with an example of how I might send the update to all browsers, every time, I'd really appreciate it.


<script language=javascript>

function longpoll(url, callback) {

    var req = new XMLHttpRequest (); ('GET', url, true); 

    req.onreadystatechange = function (aEvt) {
        if (req.readyState == 4) { 
            if (req.status == 200) {
                longpoll(url, callback);
            } else {
                alert ("long-poll connection lost");


function recv(msg) {

    var box = document.getElementById("counter");

    box.innerHTML += msg + "
function send() {

    var box = document.getElementById("counter");

  var req = new XMLHttpRequest (); ('POST', "/push?rcpt=", true); 

    req.onreadystatechange = function (aEvt) {
        if (req.readyState == 4) { 
            if (req.status == 200) {
            } else {
                alert ("failed to send!");

  //box.innerHTML += "test" ;  
<body onload="longpoll('/poll', recv);">

<h1> Long-Poll Chat Demo </h1>

<p id="counter"></p>
<button onclick="send()" id="test">Test Button</button>


package main

import (
//  "io/ioutil"

var messages chan string = make(chan string, 100)

var counter = 0

func PushHandler(w http.ResponseWriter, req *http.Request) {

    //body, err := ioutil.ReadAll(req.Body)

    /*if err != nil {
    counter += 1
    messages <- strconv.Itoa(counter)

func PollResponse(w http.ResponseWriter, req *http.Request) {

    io.WriteString(w, <-messages)

func main() {
    http.Handle("/", http.FileServer(http.Dir("./")))
    http.HandleFunc("/poll", PollResponse)
    http.HandleFunc("/push", PushHandler)
    err := http.ListenAndServe(":8010", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dse55384 2013-11-06 07:00

    The issue isn't the Go code(alone;see PS PS), it's the browser(Chrome). Making 2 requests to the same URL happens in sequence, not in parallel.

    Solution You need to add a unique timestamp to the longpoll URL to trick the browser: ('GET', url+"?"+(new Date().getTime()), true); 

    PS - I learned a lot about Go channels and mutex with this question. Thanks :)

    PS PS - James' answer ( is key to getting the server side Go code to handle more than 1 request at a time, because Go channels are blocking which means only 1 goroutine can receive at a time. So the solution to the OP's question is a combination of front and back end code changes.

    打赏 评论
  • donglin6659 2013-11-06 02:37

    Go channels are not multi-cast. That is, if you have multiple goroutines reading from the channel, only one will wake up when you write a value to the channel rather than it being broadcast to all readers.

    One alternative would be to use a condition variable instead:

    var (
        lock sync.Mutex
        cond = sync.NewCond(&lock)

    In your PollResponse handler, you can wait on the condition with:

    message := strconv.Itoa(counter)
    // write message to response

    In your PushHandler handler, you can broadcast the change with:

    counter += 1
    打赏 评论

相关推荐 更多相似问题