如何使用 Primus 中间件获取 spark 实例
How to get spark instance on using Primus middleware
我已经设置了一个 Primus websocket 服务,如下所示。
http = require('http');
server = http.createServer();
Primus = require('primus');
primus = new Primus(server, {
transformer: 'websockets',
pathname: 'ws'
});
primus.on('connection', function connection(spark) {
console.log("client has connected");
spark.write("Herro Client, I am Server");
spark.on('data', function(data) {
console.log('PRINTED FROM SERVER:', data);
spark.write('receive '+data)
});
spark.on('error', function(data) {
console.log('PRINTED FROM SERVER:', data);
spark.write('receive '+data)
});
});
server.listen(5431);
console.log("Server has started listening");
它工作正常。在上面的代码中,我使用 spark.write
向用户发送响应消息。现在我想将它转换为在中间件中使用。
代码变为如下:
primus.use('name', function (req, res, next) {
doStuff();
});
在 doStuff() 方法中,如何让 spark 实例将消息发送回客户端?
readme 对此有点模糊,但中间件只处理 HTTP 请求。
Primus has two ways of extending the functionality. We have plugins but also support middleware. And there is an important difference between these. The middleware layers allows you to modify the incoming requests before they are passed in to the transformers. Plugins allow you to modify and interact with the sparks. The middleware layer is only run for the requests that are handled by Primus.
要实现您想要的效果,您必须创建一个插件。它并不比中间件复杂多少。
primus.plugin('herro', {
server: function(primus, options) {
primus.on('connection', function(spark) {
spark.write('Herro Client, I am Server')
})
},
client: function(primus, options) {}
})
有关详细信息,请参阅自述文件的 Plugins 部分。
我已经设置了一个 Primus websocket 服务,如下所示。
http = require('http');
server = http.createServer();
Primus = require('primus');
primus = new Primus(server, {
transformer: 'websockets',
pathname: 'ws'
});
primus.on('connection', function connection(spark) {
console.log("client has connected");
spark.write("Herro Client, I am Server");
spark.on('data', function(data) {
console.log('PRINTED FROM SERVER:', data);
spark.write('receive '+data)
});
spark.on('error', function(data) {
console.log('PRINTED FROM SERVER:', data);
spark.write('receive '+data)
});
});
server.listen(5431);
console.log("Server has started listening");
它工作正常。在上面的代码中,我使用 spark.write
向用户发送响应消息。现在我想将它转换为在中间件中使用。
代码变为如下:
primus.use('name', function (req, res, next) {
doStuff();
});
在 doStuff() 方法中,如何让 spark 实例将消息发送回客户端?
readme 对此有点模糊,但中间件只处理 HTTP 请求。
Primus has two ways of extending the functionality. We have plugins but also support middleware. And there is an important difference between these. The middleware layers allows you to modify the incoming requests before they are passed in to the transformers. Plugins allow you to modify and interact with the sparks. The middleware layer is only run for the requests that are handled by Primus.
要实现您想要的效果,您必须创建一个插件。它并不比中间件复杂多少。
primus.plugin('herro', {
server: function(primus, options) {
primus.on('connection', function(spark) {
spark.write('Herro Client, I am Server')
})
},
client: function(primus, options) {}
})
有关详细信息,请参阅自述文件的 Plugins 部分。