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

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

PHP中文网
PHP中文网

认证高级PHP讲师

répondre à tous(2)
洪涛

websocket是基于http协议的。

伊谢尔伦

好奇怪,为啥给的github地址是一个java-websocket的项目,问题却出现在了nodejs版块里面。。。我晕。 Anyway,我按照nodejs来说吧!现在是的!!!

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会自动捕捉到/socket.io的请求,然后返回客户端文件给你。

Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!