node.js - Socket.io一定要绑定http模块吗?我只想建立websocket服务
PHP中文网
PHP中文网 2017-04-17 14:51:19
0
2
648

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

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
洪涛

Websocket is based on http protocol.

伊谢尔伦

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template