无需刷新页面即可将用户添加到 actioncable 频道。
Add user to actioncable channel without page refresh.
当我提到 actioncable 频道时,我指的是这个:
.
创建聊天室 + chatroom_users 后,用户必须刷新页面才能连接到特定的 chatroom_id 频道。是否可以在不重新加载页面的情况下连接到该频道?
可以根据视图中的自定义操作创建连接(而不是刷新页面)。请看下面的代码,
createconusmer = (send_params) ->
App.chatbot = App.cable.subscriptions.create { channel: "ChatbotChannel" , auth_token: send_params , url: string },
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
console.log(data)
speak: (data, responder, payload) ->
@perform 'speak' , message: data , responder: responder , payload: payload
现在您可以在咖啡文件中定义自定义函数,
nameclick = (value) ->
createconusmer value
window["nameclick"] = nameclick
现在在您的视图中,您可以使用函数 nameclick 创建一个新的流。另外,我正在添加我的代码以确保它们是否唯一,以避免添加重复连接。
connections = []
addConnection = (id) ->
connections.push(id)
removeConnection = (id) ->
index = connections.indexOf(id)
connections.splice(index, 1) if index > -1
connectedTo = (id) ->
connections.indexOf(id) > -1
nameclick = (value) ->
if connectedTo(value) == false
addConnection(value)
createconusmer value
当我提到 actioncable 频道时,我指的是这个:
创建聊天室 + chatroom_users 后,用户必须刷新页面才能连接到特定的 chatroom_id 频道。是否可以在不重新加载页面的情况下连接到该频道?
可以根据视图中的自定义操作创建连接(而不是刷新页面)。请看下面的代码,
createconusmer = (send_params) ->
App.chatbot = App.cable.subscriptions.create { channel: "ChatbotChannel" , auth_token: send_params , url: string },
connected: ->
# Called when the subscription is ready for use on the server
disconnected: ->
# Called when the subscription has been terminated by the server
received: (data) ->
console.log(data)
speak: (data, responder, payload) ->
@perform 'speak' , message: data , responder: responder , payload: payload
现在您可以在咖啡文件中定义自定义函数,
nameclick = (value) ->
createconusmer value
window["nameclick"] = nameclick
现在在您的视图中,您可以使用函数 nameclick 创建一个新的流。另外,我正在添加我的代码以确保它们是否唯一,以避免添加重复连接。
connections = []
addConnection = (id) ->
connections.push(id)
removeConnection = (id) ->
index = connections.indexOf(id)
connections.splice(index, 1) if index > -1
connectedTo = (id) ->
connections.indexOf(id) > -1
nameclick = (value) ->
if connectedTo(value) == false
addConnection(value)
createconusmer value