Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.5k views
in Technique[技术] by (71.8m points)

javascript - Can't connect to socket.io

I am trying to set up socket.io, so that I can send real-time-data to my Frontend. I read the Getting Started-Introduction from socket.io itself, but somehow it doesn't work. Whenever trying to connect to socket.io via multiple clients and also testing websites, I get a connection timeout.

I work with TypeOrm, so my startup script looks like this:

import 'reflect-metadata';

import * as express from 'express';
import * as bodyParser from 'body-parser';
import * as helmet from 'helmet';
import * as cors from 'cors';

import routes from './routes';

import { ChangeStream, createConnection } from 'typeorm';
import { createServer } from 'http';
import { Server, Socket } from 'socket.io';

createConnection()
  .then(async connection => {

    console.log('?? Connected to database!');

    const app = express();

    const httpServer = createServer(app);
    const io = new Server(httpServer);

    io.on('connection', (socket: Socket) => {
      console.log('?? Connected to Socket!');

      socket.on('disconnect', () => {
        console.log('?? Disconnected from Socket!');
      });
    });

    app.use(cors());
    app.use(helmet());
    app.use(bodyParser.json());

    app.use('/', routes);

    httpServer.listen(3000, () => {
      console.log('?? Server started on port 3000!');
    });


  })
  .catch(error => console.log(error)
);

Any help would be appreciated!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...