Code Monkey home page Code Monkey logo

hprose-nodejs's Introduction

Hprose

Promises/A+ logo

Hprose for Node.js

Join the chat at https://gitter.im/hprose/hprose-nodejs npm download optionalDependency Status npm version License



Introduction

Hprose is a High Performance Remote Object Service Engine.

It is a modern, lightweight, cross-language, cross-platform, object-oriented, high performance, remote dynamic communication middleware. It is not only easy to use, but powerful. You just need a little time to learn, then you can use it to easily construct cross language cross platform distributed application system.

Hprose supports many programming languages, for example:

  • AAuto Quicker
  • ActionScript
  • ASP
  • C++
  • Dart
  • Delphi/Free Pascal
  • dotNET(C#, Visual Basic...)
  • Golang
  • Java
  • JavaScript
  • Node.js
  • Objective-C
  • Perl
  • PHP
  • Python
  • Ruby
  • ...

Through Hprose, You can conveniently and efficiently intercommunicate between those programming languages.

This project is the implementation of Hprose for Node.js.

Hprose 2.0 for Node.js Documents(中文版): https://github.com/hprose/hprose-nodejs/wiki

Usage

Http Server

Synchronous Functions or Methods

Hprose for Node.js is very easy to use. You can create a hprose server like this:

var hprose = require("hprose");
function hello(name) {
    return "Hello " + name + "!";
}
var server = hprose.Server.create("http://0.0.0.0:8080");
server.addFunction(hello);
server.start();

To start it use:

node --harmony server.js

--harmony is a v8 options, hprose use it to optimize serialization. This is not required option, but it is recommended to use it.

Asynchronous Functions or Methods

In fact most nodejs service methods are asynchronous, you can publish asynchronous function like this:

var hprose = require("hprose");
function hello(name, callback) {
    setTimeout(function() {
        callback("Hello " + name + "!");
    }, 10);
}
var server = hprose.Server.create("http://0.0.0.0:8080");
server.addAsyncFunction(hello);
server.start();

Http Client

Then you can create a hprose client to invoke it like this:

var hprose = require("hprose");
var client = hprose.Client.create("http://127.0.0.1:8080/");
var proxy = client.useService();
proxy.hello("world", function(result) {
    console.log(result);
});

To start it use:

node --harmony client.js

or

node --harmony-proxies client.js

Without --harmony-proxies, you need create proxy like this:

var hprose = require("hprose");
var client = hprose.Client.create("http://127.0.0.1:8080/");
var proxy = client.useService(["hello"]);
proxy.hello("world", function(result) {
    console.log(result);
});

Or create client like this:

var hprose = require("hprose");
var client = hprose.Client.create("http://127.0.0.1:8080/", ["hello"]);
client.hello("world", function(result) {
    console.log(result);
});

Exception Handling

If an error occurred on the server, or your service function/method throw an exception, it will be sent to the client. You need to pass an error callback function after succuss callback function to receive it. If you omit this callback function, the client will ignore the exception, like never happened.

For example:

proxy.hello("world", function(result) {
    console.log(result);
}, function(name, err) {
    console.error(err);
});

Tcp Server & Client

The Tcp Server & Client are used as same as the Http Server & Client.

To create a Tcp Server:

var server = hprose.Server.create("tcp://0.0.0.0:4321");

To create a Tcp Client:

var client = hprose.Client.create('tcp://127.0.0.1:4321');

Unix Socket Server & Client

The Unix Socket Server & Client are used as same as the Http Server & Client.

To create a Unix Socket Server:

var server = hprose.Server.create("unix:/tmp/my.sock");

To create a Unix Socket Client:

var client = hprose.Client.create('unix:/tmp/my.sock');

WebSocket Server & Client

The WebSocket Server & Client are used as same as the Http Server & Client.

To create a WebSocket Server:

var server = hprose.Server.create("ws://0.0.0.0:8080/");

To create a WebSocket Client:

var client = hprose.Client.create('ws://0.0.0.0:8080/');

hprose-nodejs's People

Contributors

andot avatar breath-co2 avatar gitter-badger avatar wangpengju avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hprose-nodejs's Issues

client放置长时间后,回调不能执行

Hi,本人利用node-webkit在windows 7 ×64启动了一个hprose服务器(websocket),然后用nw启动了另外一个客户端(和服务不是一个进程),在放置一段时间(不到2小时)不使用后(之前正常),客户端调用服务端通过addAsyncFunction增加的方法,服务端可以正常收到消息执行,但是客户端的callback却不能被执行,请问这个问题怎么解决,还是我需要设置什么参数?

best wish!

Error in SocketClient

The current version has an problem in SocketClient.js:

node_modules/hprose/lib/client/SocketClient.js:58

        for (var key in _options) {
                        ^
ReferenceError: _options is not defined

和express配合使用

能不能和express配合使用,如果能,烦请告知;如果不能,那么能不能开发这样的功能呢

async/await不能正确工作

Nodejs 10

const hprose=require("hprose")
async function hello(name) {
return "Hello " + name + "!";
}
var server = hprose.Server("http://0.0.0.0:7511");
server.addAsyncFunction(hello,"hello");
server.start();

let client = new hprose.HttpClient("http://127.0.0.1:7511",[
"hello"
])
async function test(){
try{
console.log(await client.hello("aaaaa"))
}catch(e){
console.log(e)
}
}
test()

不能正常工作,出错TIMEOUT

用了 HTTPService 但是遇到一个问题

根据: https://github.com/hprose/hprose-nodejs/wiki/Hprose-%E6%9C%8D%E5%8A%A1%E5%99%A8
操作可以使用,但是,我不是 express koa connect,而是自己的服务器,
这种情况下, service.handle 传入的 req res 要做什么包装吗?

而且一般我们处理的是 /rpc 的路径下的请求,而不是根路由。

能不能来个原生 nodejs 的示例:

const hprose= require('hprose');
const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

var service = new hprose.HttpService();
// service.add(hello);

const server = http.createServer((req, res) => {
  // 这里 service.handle 要如何接入呢?

  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

这样也方便大家自己去封装了

Node.js客户端无法调用.NET发布的服务接口

你好,Node客户端,通过别名的方式,无法调用.NET发布的服务接口。
代码如下:
var hprose = require("hprose");

var functions = {
ISecurityRightService: ['GetSecurityRightClasses']
};

//使用构造器函数创建http客户端
var client = new hprose.HttpClient("http://localhost:8833/",functions);
var result = client.ISecurityRightService.GetSecurityRightClasses('');

console.log(result);

输出:

{ state: 'pending' }

请问发送的数据如何保证返回的结果相匹配?

假设使用hprose发送两个请求,第一个耗时较长,返回的第一个结果并不是客户端第一个请求的结果,这种情况下如何能保证请求与响应结果一一对应?

全双工套接字(Socket)绑定这个文档中,客户端发送的数据有 4 个字节表示请求的唯一标识(id),但追到源码中好像数据并没有这个id头。

请问有什么解决方案么?谢谢

请问这个模块能用于react-native中么?

在react-native项目中npm install了hprose,然后照着例子加了一个client,然后就报以下错误了,
把url,events,http,https 全部install一边还是老样子

Unable to resolve module http from /Users/xxx/works/myapp/node_modules/hprose/lib/client/HttpClient.js: Module does not exist in the module map or in these directories:
/Users/xxx/works/myapp/node_modules

This might be related to facebook/react-native#4968
To resolve try the following:

  1. Clear watchman watches: watchman watch-del-all.
  2. Delete the node_modules folder: rm -rf node_modules && npm install.
  3. Reset packager cache: rm -fr $TMPDIR/react-* or npm start -- --reset-cache.

RCTFatal + 104
-[RCTBatchedBridge stopLoadingWithError:] + 1138
__25-[RCTBatchedBridge start]_block_invoke_2 + 65
_dispatch_call_block_and_release + 12
_dispatch_client_callout + 8
_dispatch_main_queue_callback_4CF + 1054
CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE + 9
__CFRunLoopRun + 2205
CFRunLoopRunSpecific + 420
GSEventRunModal + 161
UIApplicationMain + 159
main + 111
start + 1
0x0 + 1

i can‘t use hprose with koa to share a RPC service

when i use it with koa,the nodejs will throw this exception:

Uncaught Exception:
Error: Can't set headers after they are sent.

this is my code:
it‘s could run,but can't call RPC

var app = new Koa();

function hello(name) {
    return "Hello " + name + "!";
}
const service = new hproseServer();
service.add(hello);

app.use(async (ctx: Router.IRouterContext, next: () => Promise<any>) => {
    if (ctx.path === "/api/SystemInfomation") {
        service.handle(ctx.req, ctx.res);
        return;
    }
    await next();
});
app.listen(port);

server.remove is not a function

When I try to remove a published topic via call server.remove(topic), an error raised: 'server.remove is not a function' , then I trace the source code and found the file 'Service.js' has a function named as 'remove', but not defined in 'Object.defineProperties'.

错误无法捕捉

类似代码如下:

proxy.hello("world",function(){
  throw "Hello Error!";
},function(err){//这里捕捉不到,外面try-catch也不行
  console.error("Error:", e);
});

syntaxError HarmonMaps.js: 'return' outside of function

我在使用 react native 开发移动应用,使用 npm install hprose 安装了2.0.8提示这个错误,为什么 ?
syntaxError HarmonMaps.js: 'return' outside of function

去除了 return 之后,还会提示找不到modules 'url' 与 'util' why?

使用hprose报错,源码中使用了废弃的api

我的技术栈是React+Electron,在引入hprose后控制台报错误调用了new Buffer()这个已经废弃的api,建议改为Buffer.alloc(),请问这个问题如何解决?

(node:9108) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

Typescript下没法用了

之前没有index.d.ts文件时候在Typescript可以使用。现在加了namespace之后,声明的类又没导出,请问在Typescript如何使用啊啊啊啊啊。

nodejs client 报错

var client = HproseHttpClient("xxxxx");

报错:
cannot read property 'constructor' of undefined

创建 client 完全调用不了 service,不知道如何调试,求帮忙

服务端的创建代码是:
let hproseServer = require("hprose").Server.create('tcp://0.0.0.0:' + rpcPort.toString());
hproseServer.addFunction(WhenRPC, "WhenRPC");
hproseServer.start();
console.info("创建RPC Server", myName, rpcPort);

客户端的创建代码是:

require("hprose").Client.create('tcp://127.0.0.1:' + rpcPort.toString(), ["WhenRPC"])

function WhenRPC (args, callback) {
console.info("IN CallRPCAPI", args);
//callback(null, "ReceiveSuccess");
callback("ReceiveSuccess");
}
require("hprose").Client.WhenRPC("hello",function(){

})

rpcPort 是 Number 数字

代码后台运行的一些问题

我这边写的是客户端。如果直接node client.js的话。一直都没有问题。
但转入到后台运行。关掉窗口后。立马进程就被杀死。写其他的node都没有问题啊。
转到后台运行的方法有使用如下
1 nohup node --harmony-proxies client.js >/dev/null 2>&1 &
2 node --harmony-proxies client.js &

而且使用系统的计划任务。调用shell。 在shell内运行node --harmony-proxies client.js 没有任何效果。
请教大神,是不是 --harmony-proxies 的原因。
求解决方案

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.