GithubHelp home page GithubHelp logo

Comments (2)

andot avatar andot commented on August 24, 2024

index.php

<?php
    include('HproseHttpServer.php');

    define("SEX_UNKNOWN", 0);
    define("SEX_MALE", 1);
    define("SEX_FEMALE", 2);
    define("SEX_INTERSEX", 3);

    class User {
        var $name;
        var $sex;
        var $birthday;
        var $age;
        var $married;
        function User($name, $sex, $birthday, $age, $married) {
            $this->name = $name;
            $this->sex = $sex;
            $this->birthday = $birthday;
            $this->age = $age;
            $this->married = $married;
        }
    }

    function hello($name) {
        return 'Hello ' . $name;
    }

    function sum() {
        $args = func_get_args();
        $sum = 0;
        foreach ($args as $i) {
            $sum += $i;
        }
        return $sum;
    }

    function swapKeyAndValue(&$trans) {
        $trans = array_flip($trans);
        return $trans;
    }

    function getUserList() {
        $userlist = array(
            new User("Amy", SEX_FEMALE, new HproseDate(1983, 12, 3), 26, true),
            new User("Bob", SEX_MALE, new HproseDate(1989, 6, 12), 20, false),
            new User("Chris", SEX_UNKNOWN, new HproseDate(1980, 3, 8), 29, true),
            new User("Alex", SEX_INTERSEX, new HproseDate(1992, 6, 14), 17, false)            
        );
        return $userlist;
    }  

    $server = new HproseHttpServer();
    $server->addFunction('hello');
    $server->addFunction('sum');
    $server->addFunction('swapKeyAndValue');
    $server->addFunction('getUserList');
    $server->setDebugEnabled(true);
    $server->setCrossDomainEnabled(true);    
    $server->start();
?>

demo.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta content="Content-Type" equiv="text/html; charset=utf-8" />
    <script type="text/javascript" src="hprose.js"></script>
</head>
<body>
<script type="text/javascript">
var client = new HproseHttpClient('http://127.0.0.1/index.php', ['hello', 'sum', 'swapKeyAndValue', 'getUserList']);
client.hello("World!", function(result) {
    console.log(result);
});
client.sum(1, 3, 5, function(result, args) {
    console.log(result);
    console.log(JSON.stringify(args));
});
client.swapKeyAndValue({"Jun":1, "Feb":2, "Mar": 3}, function(result, args) {
    console.log(JSON.stringify(result));
    console.log(JSON.stringify(args));
});
client.getUserList(function(result) {
    console.log(JSON.stringify(result));
});
</script>
</body>
</html>

from hprose.

bingchengcool avatar bingchengcool commented on August 24, 2024

Thank you very much for your reply,very detailed.
2013-11-26

bingchengcool

发件人:andot [email protected]
发送时间:2013-11-25 20:52
主题:Re: [hprose] do you have any demos for js client? (#6)
收件人:"andot/hprose"[email protected]
抄送:"libingcheng"[email protected]

index.php

name = $name; $this->sex = $sex; $this->birthday = $birthday; $this->age = $age; $this->married = $married; } } function hello($name) { return 'Hello ' . $name; } function sum() { $args = func_get_args(); $sum = 0; foreach ($args as $i) { $sum += $i; } return $sum; } function swapKeyAndValue(&$trans) { $trans = array_flip($trans); return $trans; } function getUserList() { $userlist = array( new User("Amy", SEX_FEMALE, new HproseDate(1983, 12, 3), 26, true), new User("Bob", SEX_MALE, new HproseDate(1989, 6, 12), 20, false), new User("Chris", SEX_UNKNOWN, new HproseDate(1980, 3, 8), 29, true), new User("Alex", SEX_INTERSEX, new HproseDate(1992, 6, 14), 17, false) ); return $userlist; } $server = new HproseHttpServer(); $server->addFunction('hello'); $server->addFunction('sum'); $server->addFunction('swapKeyAndValue'); $server->addFunction('getUserList'); $server->setDebugEnabled(true); $server->setCrossDomainEnabled(true); $server->start(); ``` ?>

demo.html

<script type="text/javascript" src="hprose.js"></script> <script type="text/javascript"> var client = new HproseHttpClient('http://127.0.0.1/index.php', ['hello', 'sum', 'swapKeyAndValue', 'getUserList']); client.hello("World!", function(result) { console.log(result); }); client.sum(1, 3, 5, function(result, args) { console.log(result); console.log(JSON.stringify(args)); }); client.swapKeyAndValue({"Jun":1, "Feb":2, "Mar": 3}, function(result, args) { console.log(JSON.stringify(result)); console.log(JSON.stringify(args)); }); client.getUserList(function(result) { console.log(JSON.stringify(result)); }); </script>


Reply to this email directly or view it on GitHub.

from hprose.

Related Issues (13)

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.