国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > JS > 正文

詳解如何使用koa實(shí)現(xiàn)socket.io官網(wǎng)的例子

2024-05-06 16:46:42
字體:
供稿:網(wǎng)友

socket.io官網(wǎng)中使用express實(shí)現(xiàn)了一個(gè)最簡單的IM即時(shí)聊天,今天我們使用koa來實(shí)現(xiàn)一下

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

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

2.在你需要的位置新建一個(gè)文件夾(官網(wǎng)的簡單命名為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

### 接下來直接上代碼

項(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);// 首頁路由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(){} 可以簡化為 () => {},寫法上更加的簡潔

頁面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)最簡單的聊天了

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JavaScript/Ajax教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 和平县| 余江县| 大同县| 丹江口市| 溧阳市| 八宿县| 贞丰县| 百色市| 鸡西市| 涞水县| 石首市| 仪征市| 六枝特区| 山东省| 雅安市| 亚东县| 浦江县| 沙坪坝区| 阿巴嘎旗| 繁昌县| 长沙市| 汝阳县| 横峰县| 宁陵县| 汾西县| 阳原县| 咸阳市| 定边县| 姚安县| 阜新市| 和平区| 扶绥县| 拜城县| 长海县| 上饶市| 商丘市| 呼伦贝尔市| 察隅县| 漳浦县| 公主岭市| 娄烦县|