pusher presence 无法使用 angularjs 和 golang 获取成员
pusher presence can't get members using angularjs and golang
到目前为止,我正在尝试使用 here 中的示例,这就是我的设置
// 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 和存在端点
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)
}
我猜这是基本形式。但我没有得到任何会员数?我可以用
从 API 得到回复
"{\"auth\":\"7d6e393c49c579c43a0c:6cd45662ef57fbc3ab16d8052c43bf95e7415846f736c2499bac7628ca3b75bc\",\"channel_data\":\"{\\"user_id\\":\\"56f9e5ffcc1cb466a624a3cf\\",\\"user_info\\":{\\"twitter\\":\\"pusher\\"}}\"}"
但有错误
Uncaught SyntaxError: Unexpected token :
我是不是遗漏了什么或者我的设置有误?
NVM,解决了问题。在 Go 示例的文档中缺少 return 它在 jsonp
中带有回调函数。响应应该是这样的...
Pusher.auth_callbacks['1']({auth: "7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75",…});
auth:"7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75"
channel_data :"{"user_id":"56fa1231cc1cb48a1cdb243c","user_info":{"twitter":"pusher"}}"
到目前为止,我正在尝试使用 here 中的示例,这就是我的设置
// 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 和存在端点
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)
}
我猜这是基本形式。但我没有得到任何会员数?我可以用
从 API 得到回复"{\"auth\":\"7d6e393c49c579c43a0c:6cd45662ef57fbc3ab16d8052c43bf95e7415846f736c2499bac7628ca3b75bc\",\"channel_data\":\"{\\"user_id\\":\\"56f9e5ffcc1cb466a624a3cf\\",\\"user_info\\":{\\"twitter\\":\\"pusher\\"}}\"}"
但有错误
Uncaught SyntaxError: Unexpected token :
我是不是遗漏了什么或者我的设置有误?
NVM,解决了问题。在 Go 示例的文档中缺少 return 它在 jsonp
中带有回调函数。响应应该是这样的...
Pusher.auth_callbacks['1']({auth: "7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75",…});
auth:"7d6e393c49c579c43a0c:33a442fcc9d3bf67febfeeb59e5a8e4f0364901069534cdef006b0b047df9f75"
channel_data :"{"user_id":"56fa1231cc1cb48a1cdb243c","user_info":{"twitter":"pusher"}}"