Socket.IO连接未建立

时间:2019-05-01 12:51:02

标签: node.js typescript express socket.io

我无法在节点服务器中建立与Socket.IO的连接。我的服务器已成功使用Socket.IO启动,但是在连接Socket的过程中,没有获取控制台日志。

 this.server.listen(this.port, () => {
        console.log(`Server is running on ${this.port} with process id ${process.pid}`)
    });

 this.io.on('connection', (socket: any) => {
        console.log(`Connected client on port ${this.port}`);

        socket.on('messsage', (msg: Message) => {
            console.log(`Data: ${msg}`);
            socket.emit('message', msg);
        });

        socket.on('disconnect', () => {
            console.log(`Client Disconnected`);
        });
    });

1 个答案:

答案 0 :(得分:1)

尝试更改您的代码: var socket = io.connect('localhost');

var socket = io.connect('http://localhost:port');