Koa v2 is now the default. For Koa v1 support install with koa-websocket@2 and see the
legacy
branch.
Supports ws://
and wss://
npm install koa-websocket
const Koa = require('koa'),
route = require('koa-route'),
websockify = require('koa-websocket');
const app = websockify(new Koa());
// Regular middleware
// Note it's app.ws.use and not app.use
app.ws.use(function(ctx, next) {
// return `next` to pass the context (ctx) on to the next ws middleware
return next(ctx);
});
// Using routes
app.ws.use(route.all('/test/:id', function (ctx) {
// `ctx` is the regular koa context created from the `ws` onConnection `socket.upgradeReq` object.
// the websocket is added to the context on `ctx.websocket`.
ctx.websocket.send('Hello World');
ctx.websocket.on('message', function(message) {
// do something with the message from client
console.log(message);
});
}));
app.listen(3000);
Example with Let's Encrypt (the Greenlock package):
const Koa = require('koa');
const greenlock = require('greenlock-express');
const websockify = require('koa-websocket');
const le = greenlock.create({
// all your sweet Let's Encrypt options here
});
// the magic happens right here
const app = websockify(new Koa(), wsOptions, le.httpsOptions);
app.ws.use((ctx) => {
// the websocket is added to the context as `ctx.websocket`.
ctx.websocket.on('message', function(message) {
// do something
});
});
app.listen(3000);
With custom websocket options.
const Koa = require('koa'),
route = require('koa-route'),
websockify = require('koa-websocket');
const wsOptions = {};
const app = websockify(new Koa(), wsOptions);
app.ws.use(route.all('/', (ctx) => {
// the websocket is added to the context as `ctx.websocket`.
ctx.websocket.on('message', function(message) {
// print message from the client
console.log(message);
});
}));
app.listen(3000);
The WebSocket options object just get passed right through to the new WebSocketServer(options)
call.
The optional HTTPS options object gets passed right into https.createServer(options)
. If the HTTPS options arepassed in, koa-websocket will use the built-in Node HTTPS server to provide support for the wss://
protocol.
MIT
最近在做运营侧中台项目的重构,目前的选型是 koa2+typescript。在实际生产中,切实体会到了 typescript 类型带来的好处。 为了更形象说明 typescript 的优势,还是先来看一个场景吧: BUG 现场 作为一门灵活度特别大的语言,坏处就是:复杂逻辑编写过程中,数据结构信息可能由于逻辑复杂、人员变更等情况而丢失,从而写出来的代码含有隐含错误。 比如这次我在给自己的博客编写
使用koa2.x koa-websocket vue2.x 1.首先 用koa创建websocket服务 /* 实例化外部依赖 */ var Koa = require("koa"); var WebSocket = require("koa-websocket"); var app = WebSocket(new Koa()); var ctxs = []; //用来保存ctx对象 用以广播信
先看看视频: websocket 广播效果 服务端程序(websocket.js): var ws = require("nodejs-websocket"); function now() { return new Date().toLocaleString(); } var n = 0; var BC = 0; function broadcast(server) {
Koa art-template view render middleware. support all feature of art-template. Install npm install --save art-template npm install --save koa-art-template Example const Koa = require('koa'); const ren
koa是Express的下一代基于Node.js的web框架,目前有1.x和2.0两个版本。 历史 1. Express Express是第一代最流行的web框架,它对Node.js的http进行了封装,用起来如下: var express = require('express'); var app = express(); app.get('/', function (req, res) {
Koa 是下一代的 Node.js 的 Web 框架。由 Express 团队设计。旨在提供一个更小型、更富有表现力、更可靠的 Web 应用和 API 的开发基础。 Koa可以通过生成器摆脱回调,极大地改进错误处理。Koa核心不绑定任何中间件,但提供了优雅的一组可以快速和愉悦地编写服务器应用的方法。 示例代码: var koa = require('koa');var app = koa();//
Koa - HelloWorld 以上便是全部了,我们重点来看示例,我们只注册一个中间件, Hello Worler Server: <?php $app = new Application(); // ... $app->υse(function(Context $ctx) { $ctx->status = 200; $ctx->body = "<h1>Hello Worl
koa-log4js A wrapper for log4js-node which support Koa logger middleware.Log message is forked from Express (Connect) logger file. Note This branch is use to Koa v2.x.To use Koa v0.x & v1.x, please ch
koa-rudy 环境 node -v >=6.9.0pm2 启动 npm install npm run dev 开发环境 npm run dev || test || prod 接口测试 npm run mocha 推荐开发工具 vscode 实现 支持 async/await MVC架构(middleware-view-controller) RESTful a