[英]WebSockets control flow using Promises
I am currently using Node.js as a server (Express.js) and AngularJs in client side, switched from HTTP-based API to WebSockets (using Socket.io) due for the performance improvement. 我目前正在使用Node.js作为服务器(Express.js),并在客户端使用AngularJs,由于性能提高,因此从基于HTTP的API切换到了WebSockets(使用Socket.io)。
Since WebSockets lacks of status codes/errors management and simply returns JSON with an optional callback, I have to implement this error management. 由于WebSockets缺少状态代码/错误管理,仅返回带有可选回调的JSON,因此我必须实现此错误管理。 I want a consistant behaviour when an error occurs, and instead of making this error check manually I want to wrap it to reduce the boilplate in my code using Promises over the bad old callback-based. 我希望在发生错误时保持一致的行为,而不是手动进行此错误检查,而不是使用Promises而不是基于错误的旧回调,将其包装起来以减少代码中的沸腾。
For example instead of using raw socket.io in angular: 例如,而不是用原始的socket.io:
socket.emit('users/read', {id: 2}, function handleResponse(res){
if(res.success){
var user = res.data;
socket.emit('projects/by-user', {userId: user.id}, function handleResponse(res){
if(res.success){
var projects = res.data;
// Do something with the data
} else{
// Error getting user's projects
console.log(res.error)
}
})
} else{
// Error getting user
console.log(res.error)
}
})
I would like to have something like this: 我想要这样的东西:
webSocketClient
.request('users/read', {id:2})
.then(function(user){
return webSocketClient.request('projects/by-user', {userId: user.id})
})
.then(function(projects){
// Do something with projects
})
.catch(function(err){
// Something went bad
})
What is the recommended JSON structure when implementing a request-response WebSockets-based API? 在实现基于请求-响应WebSockets的API时,建议的JSON结构是什么? Or there is a library that can do this for me? 还是有图书馆可以为我做到这一点?
I am currently thinks about a JSON with this structure in response: 我目前正在考虑使用以下结构的JSON:
{
success: true/false,
data: [if exists],
statusCode: [default 200 for success or 500 for failure],
error: [if exists]
}
I think that working with WebSockets using Promises instead of callbacks is a fundamental and obious requirement , but for some reason all the libraries I found didn't wrapped the callbacks with Promises, which leads me to the question if I am missing the something? 我认为使用Promises而不是回调来使用WebSockets是一项基本而苛刻的要求 ,但是由于某种原因,我发现的所有库都没有使用Promises包装回调,这使我想到了是否缺少某些东西?
Thanks 谢谢
var ownhandler={};
ownhandler.request(query){
o={};
o.query=query;
o.then=function (callback){
calltheserver(this.query,function(){
callback();
}
};
return o;
}
Im not sure if its what you want but now you can do sth like: 我不确定它是否想要什么,但是现在您可以执行以下操作:
ownhandler.request("whatever").then(function(){
//function finished
}
However you have to replace the "calltheserver()" with a real function. 但是,您必须用实际函数替换“ calltheserver()”。
Here is the way I've implemented it so far in the client side (untested), not sure if there is a better library or solution for addressing Websocket based request-response API. 到目前为止,这是我在客户端上实现的方式(未经测试),不确定是否有更好的库或解决方案可用于解决基于Websocket的请求-响应API。
Client Side Code (Angular.js) 客户端代码(Angular.js)
angular.module('app.web-sockets', [])
.factory('wsClient', function createWebSocketClient(socket, $q, assert, $timeout) {
function WebSocketClient(socket) {
this._socket = socket;
}
WebSocketClient.prototype.request = function request(url, data, options) {
var wrappedRequest = {
data: data
};
var deferred = $q.defer();
var resolved = false;
socket.emit(url, wrappedRequest, function responseCallback(response) {
if (response.success) {
deferred.resolve(response.data);
resolved = true;
} else {
deferred.reject(response);
resolved = true;
}
});
return deferred.promise;
};
return new WebSocketClient(socket);
})
; ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.