Mat*_*ann 3 javascript socket.io aurelia
我试图使用socket.io与aurelia框架.加载页面时,数据从套接字服务器中提取,但之后,它不会监听.
import io from 'socket.io-client';
var socket = io.connect( 'http://localhost:3000' );
export class Settings {
newstate = '';
constructor() {
socket.on( 'users', // <- only works once (when loading the page) but doesn't listen after
function ( userlist ) {
this.users = userlist;
}.bind( this ) );
}
addstate() {
socket.emit( 'add state', this.newstate ); // <- works flawless
this.newstate = '';
}
}
Run Code Online (Sandbox Code Playgroud)
我喜欢aurelia,但我一直没有集成socket.io.
尝试在activate()而不是constructor()中绑定您的侦听器.
import io from 'socket.io-client';
var socket = io.connect( 'http://localhost:3000' );
export class Settings {
newstate = '';
activate() {
socket.on( 'users', // <- only works once (when loading the page) but doesn't listen after
function ( userlist ) {
this.users = userlist;
}.bind( this ) );
}
addstate() {
socket.emit( 'add state', this.newstate ); // <- works flawless
this.newstate = '';
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1887 次 |
| 最近记录: |