我目前有一个angular 2+应用程序运行socket.io-client,还有一个服务器使用python和flask socketio。问题是我设法在两者之间建立了连接,客户端收到了connect事件(用on('connect')处理)。。但是,我的服务器端不接收connect事件(但是接收从客户端手动发出的其他事件)。
客户:
import { Injectable } from '@angular/core';
import { io, Socket } from 'socket.io-client';
@Injectable({
providedIn: 'root'
})
export class IoDataService {
private socket: Socket;
constructor() {
this.socket = io('http://127.0.0.1:5000', {transports:['websocket']});
this.socket.on('connect_failed', (data: Object) => {console.log(data)})
this.socket.on('error', (data: Object) => {console.log(data)})
this.socket.on('connect', () => {console.log(`connect`)})
}
...
服务器:
class MainWebApi(metaclass=Singleton):
logger = logging.getLogger('TraderWebApi')
namespace = '/'
def __init__(self):
self.app = Flask(__name__)
self.app.debug = False
self.app.config['SECRET_KEY'] = 'secret!'
self.socketio = SocketIO(self.app, cors_allowed_origins="*", async_handlers=True)
self.initialize_handlers()
self.is_started = False
self.nb_connected = 0
def start(self):
self.socketio.run(self.app, log_output=True)
self.is_started = True
def emit(self, event_name: str, data: dict, namespace: str=None):
if self.nb_connected > 0:
self.socketio.emit(event_name, data, namespace=namespace or self.namespace)
def on_connect(self):
self.nb_connected += 1
self.logger.info(f'New client connected (total {self.nb_connected})')
def on_disconnect(self):
self.nb_connected += 1
self.logger.info(f'New client connected (total {self.nb_connected})')
def subscribe_listener(self, event_name: str, method: Callable, namespace: str=None):
self.socketio.on(event_name, namespace=namespace or self.namespace)(method)
def initialize_handlers(self):
self.subscribe_listener('connect', self.on_connect)
self.subscribe_listener('disconnect', self.on_disconnect)
暂无答案!
目前还没有任何答案,快来回答吧!