O_Schnappi
2018-06-12 17:16
var app = require('http').createServer() var io = require('socket.io')(app); var PORT = 3000; //客户端计数 var clientCount = 0; //存储客户端socket var socketMap = {}; app.listen(PORT); io.on('connection', function (socket) { // 这个进不去,coonection 失败,连不上socket.io clientCount += 1; socket.clientNum = clientCount; socketMap[clientCount] = socket; console.log(clientCount); if(clientCount % 2 == 1) { socket.emit('wwaiting', 'waitiong for another person'); }else { socket.emit('start'); socketMap[(clientCount - 1)].emit('start'); } socket.on('disconnect', function() { }); socket.emit('news', { hello: 'world' }); socket.on('my other event', function (data) { console.log(data); }); }); console.log('websocket listening on port ' + PORT);
解决啦!
<script src="socket.io-2.0.3.js"></script> <script src="js/script.js"></script>
这两个引入文件的引入先后顺序放了,在加载script 之前应该先加载socket.io.js 文件
基于websocket的火拼俄罗斯(升级版)
21259 学习 · 15 问题
相似问题