search

Home  >  Q&A  >  body text

node.js - Socket.io一定要绑定http模块吗?我只想建立websocket服务

https://github.com/TooTallNat...

PHP中文网PHP中文网2780 days ago623

reply all(2)I'll reply

  • PHP中文网

    PHP中文网2017-04-17 14:53:19

    Websocket is based on http protocol.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:53:19

    It’s strange, why the github address given is a java-websocket project, but the problem appears in the nodejs section. . . I'm dizzy. Anyway, let me tell you based on nodejs! Now yes! ! !

    the back-end js:

    var express = require('express')
    var app = express();
    var http = require('http').Server(app);
    var io = require('socket.io')(http);
    
    app.use(express.static(__dirname));
    app.get('/', function(req, res){
        res.sendFile(__dirname + '/index.html');
    });
    
    io.on('connection', function(socket){
        console.log('a user connected');
    });
    
    http.listen(8888, function() {
        console.log('listening on *:8888');
    });

    the front-end html file

    <html>
    <body>
      <!-- add ur js file here, it server as static content from ur back-end service(express static middleware)-->
      <!--<script src='/js/***.js'></script>-->
      <script src='/socket.io/socket.io.js'></script>
    </body>
    </html>
    
    • socket.io will automatically capture /socket.io’s request and return the client file to you.

    reply
    0
  • Cancelreply