使用koa實(shí)現(xiàn)socket.io官網(wǎng)的案例-創(chuàng)新互聯(lián)

這篇文章主要介紹使用koa實(shí)現(xiàn)socket.io官網(wǎng)的案例,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

創(chuàng)新互聯(lián)建站提供高防服務(wù)器、云服務(wù)器、香港服務(wù)器、服務(wù)器托管

### 框架準(zhǔn)備

1.確保你本地已經(jīng)安裝好了nodejs和npm,使用koa要求node版本>7.6

2.在你需要的位置新建一個(gè)文件夾(官網(wǎng)的簡(jiǎn)單命名為chat-example)

3.進(jìn)入項(xiàng)目目錄,創(chuàng)建package.json文件:

{
 "name": "socket-chat-example",
 "version": "0.0.1",
 "description": "my first socket.io app",
 "dependencies": {}
}

4.命令行中使用npm安裝,執(zhí)行以下命令

npm install --save koa koa-router http fs socket.io

### 接下來(lái)直接上代碼

項(xiàng)目目錄下直接新建index.js

var Koa = require('koa');
var app = new Koa();
const Router = require('koa-router');
const fs = require('fs');
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);

// 首頁(yè)路由
let router = new Router();
router.get('/', ctx => {
  ctx.response.type = 'html';
  ctx.response.body = fs.createReadStream('./index.html');
});
app.use(router.routes());

// socket連接
io.on('connection', (socket) => {
  socket.on('chat message', (msg) => {
    console.log('message: '+msg);
    io.emit('chat message', msg);
  });
  socket.on('disconnect', () => {
    console.log('user disconnected');
  });
});

// 監(jiān)聽端口
server.listen(3000, () => {
  console.log('listening on *:3000');
});

重點(diǎn):

socket的連接方式是先建立server,它的獲取方式不再是:

var http = require('http').Server(app);
var io = require('socket.io')(http);

而變成了:

const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);

node8之后,function(){} 可以簡(jiǎn)化為 () => {},寫法上更加的簡(jiǎn)潔

頁(yè)面index.html

<!doctype html>
<html>
 <head>
  <title>Socket.IO chat</title>
  <style>
   * { margin: 0; padding: 0; box-sizing: border-box; }
   body { font: 13px Helvetica, Arial; }
   form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
   form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
   form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
   #messages { list-style-type: none; margin: 0; padding: 0; }
   #messages li { padding: 5px 10px; }
   #messages li:nth-child(odd) { background: #eee; }
  </style>
 </head>
 <body>
  <ul id="messages"></ul>
  <form action="">
   <input id="m" autocomplete="off" /><button>Send</button>
  </form>
  <script src="/socket.io/socket.io.js"></script>
  <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
   <script>
     $(function () {
       var socket = io();
       $('form').submit(function(){
         socket.emit('chat message', $('#m').val());
         $('#m').val('');
         return false;
       });
       socket.on('chat message', function(msg){
         $('#message').append($('<li>').text(msg));
       });
     });
   </script>
 </body>
</html>

index.html和官網(wǎng)的一樣,不做任何改動(dòng)

最后執(zhí)行node index.js,打開瀏覽器,輸入http://localhost:3000就可以實(shí)現(xiàn)最簡(jiǎn)單的聊天了

以上是“使用koa實(shí)現(xiàn)socket.io官網(wǎng)的案例”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

文章標(biāo)題:使用koa實(shí)現(xiàn)socket.io官網(wǎng)的案例-創(chuàng)新互聯(lián)
文章來(lái)源:http://bm7419.com/article46/geeeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營(yíng)銷推廣、網(wǎng)站收錄、ChatGPT、虛擬主機(jī)、網(wǎng)站建設(shè)、服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開發(fā)