douyoufan7881 2016-03-29 02:46
浏览 63
已采纳

推送器存在无法使用angularjs和golang获取成员

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

// angularjs

    // 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 and the presence endpoint

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{
            "twitter": "pusher",
        },
    }
    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

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

but has error

Uncaught SyntaxError: Unexpected token :

Am I missing something or my setup is wrong?

  • 写回答

1条回答 默认 最新

  • douchangmian0305 2016-03-29 06:24
    关注

    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['1']({auth: "7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75",…});
    auth:"7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75"
    channel_data :"{"user_id":"56fa1231cc1cb48a1cdb243c","user_info":{"twitter":"pusher"}}"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?