检测何时使用 ember 更改套接字 io 连接

Detect when a socket io connection has been changed with ember

我将 ember 与 socket.io 一起使用,我想要一个计算的 属性,如果套接字 io 连接已连接或断开连接,它会更改为。

我正在使用 ember-websockets,这是我尝试过的:

socketIOService: service('socket-io'),
socketRoute:     'http://localhost:8080/',

connected: computed('socketIOService',
function()
{
    console.log('changed!');
    //return (this.get('socketIOService').socketFor(this.get('socketRoute').socket.connected));
}),

startConnection()
{
    this.get('connected');
    const socket = this.socketIOService.socketFor(this.get('socketRoute'));

    socket.on('initialised', this.initialised, this);
},

所以这不起作用,因为我猜服务不会改变。我希望能够从以下计算出一个值...

this.socketIOService.socketFor(this.get('socketRoute'));

但是我无法在计算 属性 中得到 sockerFor 属性。

查看 readme,我认为您可以使用 'open' 和 'close' 事件,w/co 计算属性:

startConnection()
{
    const socket = this.socketIOService.socketFor(this.get('socketRoute'));
    socket.on('open', () => { this.set('connected', true); });
    socket.on('close', () => { this.set('connected', false); });
}