最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ruby on rails - Use action cable with a standalone javascript application - Stack Overflow

programmeradmin1浏览0评论

I have this channel.

class TestChannel < ApplicationCable::Channel
    def subscribed
      Rails.logger.info 'subscribed to test_channel yuppi'
      stream_for current_use
      transmit({ message: 'Welcome to the test channel!' })
    end

    def unsubscribed
      # Any cleanup needed when channel is unsubscribed
    end

Then I am trying to use this from a javascript application

const socket = new WebSocket('wss://petros-test/cable/? 
token=1234567890&channel=TestChannel');

socket.onopen = function() {
  document.getElementById('status').innerText = 'Connected to cable';
  console.log('WebSocket connection established');
};

socket.onmessage = function(event) {
  console.log('Message from server:', event.data);
};

The connection is been established but without invoking the correct channel, only a general connection is established and I am getting this on the logs

WebSocket connection established
  (index):23 Message from server: {"type":"welcome"}
  (index):23 Message from server: {"type":"ping","message":1741205085}
发布评论

评论列表(0)

  1. 暂无评论