Guest User

Untitled

a guest
Jul 23rd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import ActionCable from 'actioncable'
  2.  
  3. const buildUrl = (url, parameters) {
  4. var qs = ''
  5. for (var key in parameters) {
  6. var value = parameters[key]
  7. qs += encodeURIComponent(key) + '=' + encodeURIComponent(value) + '&'
  8. }
  9. if (qs.length > 0) {
  10. qs = qs.substring(0, qs.length - 1)
  11. url = url + '?' + qs
  12. }
  13. return url
  14. }
  15.  
  16. const tokens = {
  17. 'access-token': 'xxx',
  18. 'client': 'xxx',
  19. 'uid': 'xxx'
  20. }
  21. const wsUrl = buildUrl(
  22. 'ws://localhost:3000/cable',
  23. tokens
  24. )
  25. const consumer = ActionCable.createConsumer(wsUrl)
  26. const channel = consumer.subscriptions.create({
  27. channel: 'HogeChannel'
  28. }, {
  29. connected: (o) => console.log(o),
  30. disconnected: (o) => console.log(o),
  31. received: (o) => console.log(o),
  32. rejected: (o) => console.log(o)
  33. })
  34.  
  35. // Send action
  36. channel.perform('hoge')
Add Comment
Please, Sign In to add comment