【问题标题】:convert to typescript - socketio + angular + node.js转换为打字稿 - socketio + angular + node.js
【发布时间】:2021-10-05 09:47:54
【问题描述】:

这是我的节点服务器文件 -

import http from 'http';
import Debug from 'debug';

import socketio, { Server } from 'socket.io';

import app from './app';

import ServerGlobal from './server-global';
import createSocket from './socket-manager';

const debug = Debug('node-angular');

const normalizePort = (val: string) => {
    var port = parseInt(val, 10);

    if (isNaN(port)) {
        return val;
    }

    if (port >= 0) {
        return port;
    }

    return null;
};

const onError = (error: NodeJS.ErrnoException) => {
    if (error.syscall !== 'listen') {
        throw error;
    }

    const addr = server.address();
    const bind = typeof addr === 'string' ? ('pipe ' + addr) : ('port ' + port);

    switch (error.code) {
        case 'EACCES':
            console.error(bind + 'requires elevated privileges');
            process.exit(1);
            break;
        case 'EADDRINUSE':
            console.error(bind + ' is already in use');
            process.exit(1);
            break;
        default:
            throw error;
    }
};

const onListening = () => {
    const addr = server.address();
    const bind = typeof addr === 'string' ? ('pipe ' + addr) : ('port ' + port);

    debug("Listening on " + bind);
};

const port = normalizePort(process.env.PORT);

app.set('port', port);

const server = http.createServer(app);
const io = new socketio.Server(server, {
    cors: {
        origin: 'http://localhost:4200',
    },
});

io.on('connection', () => console.log(33333));

server.on('error', onError);
server.on('listening', onListening);
server.listen(port);

ServerGlobal.getInstance().logger.info(`Server is running on port ${process.env.PORT}`);

并且套接字不会返回 console.log 但如果我使用它 -

const io = require('socket.io')(server, {
    cors: {
        origins: ['http://localhost:4200']
    }
});

我在控制台上获取日志。

如何将其转换为 ts?在 socket.io 文档上没有找到任何东西 ..................................................... ......................................... ..................................................... ......................................... ..................................................... ....................

【问题讨论】:

    标签: javascript node.js angular typescript socket.io


    【解决方案1】:

    我认为socketio的Serve是一个接口。试试const io = socketio(server) intead const io = socketio.Server(server)

    【讨论】:

      猜你喜欢
      • 2017-12-22
      • 2019-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2018-06-14
      • 2022-01-24
      相关资源
      最近更新 更多