詳解如何使用koa實(shí)現(xiàn)socket.io官網(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í)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Node.js和Express中設(shè)置TypeScript的實(shí)現(xiàn)步驟
本文主要介紹了Node.js和Express中設(shè)置TypeScript的實(shí)現(xiàn)步驟文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11
詳解阿里Node.js技術(shù)文檔之process模塊學(xué)習(xí)指南
這篇文章主要介紹了詳解阿里Node.js技術(shù)文檔之process模塊學(xué)習(xí)指南,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
node.js中對(duì)Event Loop事件循環(huán)的理解與應(yīng)用實(shí)例分析
這篇文章主要介紹了node.js中對(duì)Event Loop事件循環(huán)的理解與應(yīng)用,結(jié)合實(shí)例形式分析了node.js中Event Loop事件循環(huán)相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2020-02-02
nodejs npm錯(cuò)誤Error:UNKNOWN:unknown error,mkdir ''D:\Develop\n
今天小編就為大家分享一篇關(guān)于nodejs npm錯(cuò)誤Error:UNKNOWN:unknown error,mkdir 'D:\Develop\nodejs\node_global'at Error,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03
基于nodejs+express(4.x+)實(shí)現(xiàn)文件上傳功能
通過一段時(shí)間的查閱資料發(fā)現(xiàn)實(shí)現(xiàn)上傳的方式有:1.express中間件multer模塊2.connect-multiparty模塊(但現(xiàn)在 官方不推薦 )3.使用multiparty模塊實(shí)現(xiàn)4.使用formidable插件實(shí)現(xiàn),本文給大家介紹nodejs+express(4.x+)實(shí)現(xiàn)文件上傳功能,需要的朋友參考下2015-11-11
基于Node.js實(shí)現(xiàn)壓縮和解壓縮的方法
本篇文章主要介紹了基于Node.js實(shí)現(xiàn)壓縮和解壓縮的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-02-02
使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目
這篇文章介紹了使用Make構(gòu)建Node.js網(wǎng)站項(xiàng)目的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
Node.js開發(fā)之訪問Redis數(shù)據(jù)庫教程
這篇文章主要介紹了Node.js開發(fā)之訪問Redis數(shù)據(jù)庫教程,本文講解了安裝Redis的Node.js驅(qū)動(dòng)、編寫測試程序以及npm遠(yuǎn)程服務(wù)器連接十分緩慢的解決方法,需要的朋友可以參考下2015-01-01

