原文鏈接:?socket.io官方文檔中文版 - 陳帥華 / 探索技術(shù)、藝術(shù)與國(guó)學(xué)之美
最近對(duì)實(shí)時(shí)通信感興趣锅尘,就研究socket.io的官方文檔娩践,讀完之后覺得也就幾個(gè)常用的方法來回的調(diào)妖异,關(guān)鍵是能在實(shí)際應(yīng)用場(chǎng)景中玩出花樣來。帥華君在閱讀文檔的過程中順便把官方文檔翻譯成中文,方便初學(xué)者入門奇钞,不過建議還是要去socket.io官網(wǎng)看看忽刽。
本文檔長(zhǎ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');