推送器(pusher)的存在无法使用AngularJS和Golang获取成员。

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

pusher presence can't get members using angularjs and golang

问题

我正在尝试使用这里的示例,目前我已经设置了以下内容:

// angularjs
<!-- language: lang-js -->

// JS
var presenceClient = new Pusher('API_KEY', {
authEndpoint: apiServer + "/presence_auth",
authTransport: 'jsonp',
encrypted: true
})

var c = pusher.subscribe("presence-testchan")
Utils.log(c.members.count) // 0
Utils.log("000============")
c.bind('pusher:subscription_succeeded', function(members) {
Utils.log("succeeded?")
Utils.log(members)
})

// golang和presence端点

<!-- language: go -->

func Presence(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
params, _ := GetBytes(r.URL.String())

presenceData := pusher.MemberData{
    UserId: database.GenerateStrObjID(),
    UserInfo: map[string]string{
        &quot;twitter&quot;: &quot;pusher&quot;,
    },
}
response, err := PusherClient.AuthenticatePresenceChannel(params, presenceData)

if err != nil {
    panic(err)
}

respondToJSON(w, string(response))

}

func respondToJSON(w http.ResponseWriter, data interface{}) {

r := render.New()
r.JSON(w, http.StatusOK, data)

}

我猜这是基本形式。但是我没有得到任何成员计数?我可以从API获得响应:

"{&quot;auth&quot;:&quot;7d6e393c49c579c43a0c:6cd45662ef57fbc3ab16d8052c43bf95e7415846f736c2499bac7628ca3b75bc&quot;,&quot;channel_data&quot;:&quot;{\&quot;user_id\&quot;:\&quot;56f9e5ffcc1cb466a624a3cf\&quot;,\&quot;user_info\&quot;:{\&quot;twitter\&quot;:\&quot;pusher\&quot;}}&quot;}"

但是出现错误:

Uncaught SyntaxError: Unexpected token :

我是否遗漏了什么或者我的设置有问题?

英文:

I am trying to use the examples from here so far this is what I have setup

// angularjs
<!-- language: lang-js -->

    // JS
    var presenceClient = new Pusher(&#39;API_KEY&#39;, {
        authEndpoint: apiServer + &quot;/presence_auth&quot;,
        authTransport: &#39;jsonp&#39;,
        encrypted: true            
    })


    var c = pusher.subscribe(&quot;presence-testchan&quot;)
    Utils.log(c.members.count) // 0
    Utils.log(&quot;000============&quot;)    
    c.bind(&#39;pusher:subscription_succeeded&#39;, function(members) {
        Utils.log(&quot;succeeded?&quot;)
        Utils.log(members)
    })

// golang and the presence endpoint

<!-- language: go -->

func Presence(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
    params, _ := GetBytes(r.URL.String())

    presenceData := pusher.MemberData{
        UserId: database.GenerateStrObjID(),
        UserInfo: map[string]string{
            &quot;twitter&quot;: &quot;pusher&quot;,
        },
    }
    response, err := PusherClient.AuthenticatePresenceChannel(params, presenceData)

    if err != nil {
        panic(err)
    }

    respondToJSON(w, string(response))
}


func respondToJSON(w http.ResponseWriter, data interface{}) {

    r := render.New()
    r.JSON(w, http.StatusOK, data)
}

I guess that is the basic form. but I don't get any members count?. I can get a response from the API with

&quot;{\&quot;auth\&quot;:\&quot;7d6e393c49c579c43a0c:6cd45662ef57fbc3ab16d8052c43bf95e7415846f736c2499bac7628ca3b75bc\&quot;,\&quot;channel_data\&quot;:\&quot;{\\\&quot;user_id\\\&quot;:\\\&quot;56f9e5ffcc1cb466a624a3cf\\\&quot;,\\\&quot;user_info\\\&quot;:{\\\&quot;twitter\\\&quot;:\\\&quot;pusher\\\&quot;}}\&quot;}&quot;

but has error

Uncaught SyntaxError: Unexpected token :

Am I missing something or my setup is wrong?

答案1

得分: 0

NVM,问题已解决...在Go示例文档中缺少将其以jsonp和回调函数返回的部分。响应应该包含类似以下内容的内容...

Pusher.auth_callbacks['1']({auth: "7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75",...});
auth:"7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75"
channel_data :"{"user_id":"56fa1231cc1cb48a1cdb243c","user_info":{"twitter":"pusher"}}"
英文:

NVM, solved the problem.. it's missing in the docs for Go examples to return it in jsonp with callback func. response should have something like this...

Pusher.auth_callbacks[&#39;1&#39;]({auth: &quot;7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75&quot;,…});
auth:&quot;7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75&quot;
channel_data :&quot;{&quot;user_id&quot;:&quot;56fa1231cc1cb48a1cdb243c&quot;,&quot;user_info&quot;:{&quot;twitter&quot;:&quot;pusher&quot;}}&quot;

huangapple
  • 本文由 发表于 2016年3月29日 10:46:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/36274476.html
匿名

发表评论

匿名网友

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

确定