原文鏈接:?socket.io官方文檔中文版 - 陳帥華 / 探索技術(shù)、藝術(shù)與國學(xué)之美

最近對實時通信感興趣,就研究socket.io的官方文檔,讀完之后覺得也就幾個常用的方法來回的調(diào),關(guān)鍵是能在實際應(yīng)用場景中玩出花樣來。帥華君在閱讀文檔的過程中順便把官方文檔翻譯成中文,方便初學(xué)者入門,不過建議還是要去socket.io官網(wǎng)看看。
本文檔長期更新
目錄
1、如何使用
安裝
$npm install socket.io
使用Node http服務(wù)器搭建
服務(wù)器端(app.js)
varapp =require('http').createServer(handler)vario =require('socket.io')(app);varfs =require('fs');app.listen(80);functionhandler(req, res){? fs.readFile(__dirname +'/index.html',function(err, data){if(err) {? ? ? res.writeHead(500);returnres.end('Error loading index.html');? ? }? ? res.writeHead(200);? ? res.end(data);? });}io.on('connection',function(socket){? socket.emit('news', {hello:'world'});? socket.on('my other event',function(data){console.log(data);? });});
客戶端(index.html)
varsocket = io('http://localhost');
varsocket = io('http://localhost');varsocket = io('http://localhost');