Angular Socket.Io未在Chrome中触发任何事件
我正在尝试在Angular应用程序中建立Socket.IO连接,但无法使其工作!这是我目前的代码:Angular Socket.Io未在Chrome中触发任何事件,angular,socket.io,Angular,Socket.io,我正在尝试在Angular应用程序中建立Socket.IO连接,但无法使其工作!这是我目前的代码: import { io } from 'socket.io-client'; var sock = io('http://my.socket.server', { reconnectionAttempts: 10, }); sock.on('connect', () => { console.log('connect'); }); s
import { io } from 'socket.io-client';
var sock = io('http://my.socket.server', {
reconnectionAttempts: 10,
});
sock.on('connect', () => { console.log('connect'); });
sock.on('reconnect_attempt', () => { console.log('recon'); });
sock.on('reconnecting', () => { console.log('recon'); });
sock.onAny(() => { console.log('any'); });
sock.close();
我可以看到浏览器正在尝试连接,我甚至可以设置重新连接的尝试,这样就可以工作了。但是没有调用我的appender上的!这可能是一个非常琐碎的问题
谢谢大家!
编辑:现在尝试此代码,但我也没有看到任何事件:
var sock = io('https://socketio-chat-h9jt.herokuapp.com/', {
reconnectionAttempts: 5,
});
sock.on('connection', sock => {
alert(1);
sock.on('connect', () => { console.log('connect'); });
sock.on('reconnect_attempt', () => { console.log('recon'); });
sock.on('reconnecting', () => { console.log('recon'); });
sock.onAny(() => { console.log('any'); });
sock.close();
});
如果这是客户端,那么根据socket.io文档,语法应该如下
const io = require('socket.io')(3000);
io.on('connection', socket => {
socket.on('message', (data) => {
console.log(data);
});
});
因此,所有socket.on函数都应该位于一个更大的io.on('connection')函数中。因此,如果根本没有建立连接,就不可能出现连接错误?是的,如果这是客户端javascript。