尊龙凯时人生就是博

SwooleʵÏÖ¸ßÐÔÄܵÄHTML5ÓÎϷЧÀÍÆ÷

Ëæ×Åhtml5ÊÖÒÕµÄÆÕ¼°ºÍÉú³¤ £¬Ô½À´Ô½¶àµÄÓÎÏ·×îÏȽÓÄÉhtml5ÊÖÒÕÀ´¹¹½¨ÓÎÏ·¿Í»§¶Ë  ¡£html5ÊÖÒÕµÄÓŵãÔÚÓÚ¿çƽ̨¡¢¿ç×°±¸¡¢ÎÞÐè×°Öòå¼þµÈÌØÕ÷  ¡£È»¶ø £¬html5ÓÎÏ·µÄЧÀÍÆ÷¶ËÒÀÈ»ÊǸöÄѵã  ¡£ÔÚwebЧÀÍÆ÷¿ò¼ÜÖÐͨ³£Ê¹ÓÃphp¡¢node.jsµÈ±à³ÌÓïÑÔÀ´ÊµÏÖЧÀÍÆ÷¶ËÂß¼­  ¡£È»¶ø £¬ÕâЩ¹Å°åµÄwebЧÀÍÆ÷¿ò¼Ü¶¼²»ÊÇΪ¸ß²¢·¢ºÍʵʱ½»»¥¶øÉè¼ÆµÄ  ¡£

ΪÏàʶ¾öÕâ¸öÎÊÌâ £¬Swoole×÷Ϊһ¸ö¸ßÐÔÄܵÄÍøÂçͨѶ¿ò¼Ü £¬ÔÚ2015Äê×îÏȶÔHTML5ÓÎϷЧÀÍÆ÷µÄÖ§³Ö  ¡£SwooleÔÚÍøÂçͨѶ·½ÃæÓкÜÇ¿µÄÓÅÊÆ £¬Ëü»ùÓÚÒì²½ÊÂÎñÇý¶¯µÄ±à³Ìģʽ £¬ÍêÈ«Ö§³ÖPHPЭ³Ì £¬ÔÚÍøÂçIO÷缯Ð͵ij¡¾°Ï¾ßÓо«²ÊµÄÐÔÄܺÍÎȹÌÐÔ  ¡£

ÏÂÃæÖ÷ÒªÏÈÈÝÔõÑùʹÓÃSwooleÀ´ÊµÏÖÒ»¸ö¸ßÐÔÄܵÄHTML5ÓÎϷЧÀÍÆ÷  ¡£

Ò»¡¢Swoole¼ò½é

SwooleÊÇPHPµÄÒ»¸ö¿ªÔ´ÍøÂçͨѶ¿ò¼Ü £¬Ö§³ÖÒì²½ºÍЭ³ÌÁ½ÖÖ±à³Ìģʽ  ¡£Ëü¿ÉÒÔÓÃÓÚ¹¹½¨TCP¡¢UDP¡¢Unix SocketµÈ¶àÖÖÓ¦Óó¡¾° £¬ÀýÈçWebЧÀÍÆ÷¡¢RPCЧÀÍÆ÷¡¢ÓÎϷЧÀÍÆ÷µÈ  ¡£SwooleÌṩÁ˸»ºñµÄ½Ó¿ÚºÍÊÂÎñ»Øµ÷º¯Êý £¬¿ÉÒÔÀû±ãµØʵÏָ߲¢·¢ºÍʵʱ½»»¥µÄÓ¦ÓóÌÐò  ¡£

Á¬Ã¦Ñ§Ï°¡°Ç°¶ËÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

¶þ¡¢HTML5ÓÎϷЧÀÍÆ÷µÄ¼Ü¹¹

HTML5ÓÎϷͨ³£½ÓÄÉ¿Í»§¶Ë-ЧÀÍÆ÷ģʽ £¬¿Í»§¶ËʹÓÃHTML5¡¢CSS3ºÍJavaScriptµÈÊÖÒÕ¹¹½¨ÓÎÏ·½çÃæºÍÂß¼­ £¬Ð§ÀÍÆ÷ÈÏÕæ´¦ÀíÓÎÏ·Âß¼­¡¢´æ´¢ÓÎÏ·Êý¾ÝºÍÓë¿Í»§¶Ë¾ÙÐÐʵʱͨѶ  ¡£

ÔÚHTML5ÓÎϷЧÀÍÆ÷µÄʵÏÖÖÐ £¬Í¨³£·ÖΪÁ½²ã£ºÓ¦ÓòãºÍÍøÂç²ã  ¡£Ó¦ÓòãÈÏÕæ´¦ÀíÓÎÏ·Âß¼­ºÍÊý¾Ý´æ´¢ £¬Í¨³£½ÓÄÉPHP¡¢Java¡¢PythonµÈ±à³ÌÓïÑÔÀ´ÊµÏÖ £»ÍøÂç²ãÈÏÕæ´¦Àí¿Í»§¶ËºÍЧÀÍÆ÷Ö®¼äµÄͨѶ £¬Í¨³£Ê¹ÓÃTCP»òUDPЭÒéÀ´´«ÊäÊý¾Ý  ¡£

Swoole¿ÉÒÔ×÷ΪHTML5ÓÎϷЧÀÍÆ÷µÄÍøÂç²ã £¬ËüÌṩÁËTCPºÍUDPµÄÖ§³Ö £¬²¢ÇÒÖ§³ÖWebSocketЭÒé  ¡£SwooleµÄ¸ß²¢·¢ºÍʵʱ½»»¥ÌØÕ÷ºÜÊÇÊʺÏHTML5ÓÎϷЧÀÍÆ÷µÄʵÏÖ  ¡£

Èý¡¢SwooleʵÏÖHTML5ÓÎϷЧÀÍÆ÷µÄʾÀý

ÏÂÃæÊÇÒ»¸ö¼òÆÓµÄʹÓÃSwooleʵÏÖHTML5ÓÎϷЧÀÍÆ÷µÄʾÀý  ¡£¸ÃʾÀý½ÓÄÉTCPЭÒé¾ÙÐÐͨѶ £¬²¢Ê¹ÓÃJSONÃûÌÃ×÷ΪÊý¾ÝµÄ½»Á÷ÃûÌà  ¡ £¿Í»§¶ËʹÓÃHTML5ºÍJavaScriptʵÏÖ £¬Ð§ÀÍÆ÷¶ËʹÓÃPHPºÍSwooleʵÏÖ  ¡£

ЧÀÍÆ÷¶Ë´úÂ루server.php£©£º

<?php

// ½¨ÉèÒ»¸öTCPЧÀÍÆ÷¹¤¾ß
$server = new SwooleServer('127.0.0.1', 9501, SWOOLE_PROCESS, SWOOLE_SOCK_TCP);

// ÉèÖÃÔËÐÐʱ²ÎÊý
$server->set([
    'worker_num' => 4,
]);

// ¼àÌýÅþÁ¬ÊÂÎñ
$server->on('connect', function($server, $fd) {
    echo "Client connected: $fd
";
});

// ¼àÌýÊý¾ÝÎüÊÕÊÂÎñ
$server->on('receive', function($server, $fd, $data) {
    // ÆÊÎö¿Í»§¶Ë·¢Ë͵ÄJSONÃûÌõÄÊý¾Ý
    $json = json_decode($data, true);
    if ($json) {
        $action = $json['action'];
        $params = $json['params'];
        switch ($action) {
            case 'login':
                // ´¦ÀíÓû§µÇ¼Âß¼­
                // ...
                // ·¢Ë͵ǼÀֳɵÄÐÂÎÅ
                $response = [
                    'code' => 0,
                    'msg' => 'Login success',
                ];
                $server->send($fd, json_encode($response));
                break;
            case 'chat':
                // ´¦ÀíÓû§Ì¸ÌìÐÂÎÅ
                // ...
                // ·¢ËÍ̸ÌìÐÂΟøËùÓÐÔÚÏßÓû§
                $response = [
                    'code' => 0,
                    'msg' => 'Send message success',
                ];
                $server->send(json_encode($response));
                break;
            default:
                // ´¦Àíδ֪ÇëÇó
                // ...
                break;
        }
    } else {
        // ´¦ÀíÎÞЧÊý¾Ý
        // ...
        $server->close($fd);
    }
});

// ¼àÌýÅþÁ¬¶Ï¿ªÊÂÎñ
$server->on('close', function($server, $fd) {
    echo "Client disconnected: $fd
";
});

// Æô¶¯Ð§ÀÍÆ÷
$server->start();

µÇ¼ºó¸´ÖÆ

¿Í»§¶Ë´úÂ루client.html£©£º

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>HTML5 Game Client</title>
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
    <div>
        <label for="username">Username:</label>
        <input id="username" type="text">
    </div>
    <div>
        <label for="password">Password:</label>
        <input id="password" type="password">
    </div>
    <div>
        <button id="login">Login</button>
    </div>
    <div>
        <textarea id="chat" cols="50" rows="10"></textarea>
    </div>
    <div>
        <label for="message">Message:</label>
        <input id="message" type="text">
        <button id="send">Send</button>
    </div>
    <script>
        // ½¨ÉèÒ»¸öTCPÅþÁ¬¹¤¾ß
        var socket = new WebSocket('ws://127.0.0.1:9501');

        // ¼àÌýÅþÁ¬·­¿ªÊÂÎñ
        socket.addEventListener('open', function(event) {
            console.log('Connection opened', event);
        });

        // ¼àÌýÐÂÎÅÎüÊÕÊÂÎñ
        socket.addEventListener('message', function(event) {
            console.log('Message received', event);
            var json = JSON.parse(event.data);
            var code = json.code;
            var msg = json.msg;
            switch (code) {
                case 0:
                    // ´¦ÀíÀÖ³ÉÐÂÎÅ
                    // ...
                    break;
                default:
                    // ´¦Àíʧ°ÜÐÂÎÅ
                    // ...
                    break;
            }
        });

        // ¼àÌýÅþÁ¬¹Ø±ÕÊÂÎñ
        socket.addEventListener('close', function(event) {
            console.log('Connection closed', event);
        });

        // ¼àÌý¹ýʧÊÂÎñ
        socket.addEventListener('error', function(event) {
            console.log('Connection error', event);
        });

        // ´¦ÀíµÇ¼ÇëÇó
        $('#login').click(function() {
            var username = $('#username').val();
            var password = $('#password').val();
            var request = {
                action: 'login',
                params: {
                    username: username,
                    password: password,
                }
            }
            socket.send(JSON.stringify(request));
        });

        // ´¦Àí·¢ËÍÐÂÎÅÇëÇó
        $('#send').click(function() {
            var message = $('#message').val();
            var request = {
                action: 'chat',
                params: {
                    message: message,
                }
            }
            socket.send(JSON.stringify(request));
        });
    </script>
</body>
</html>

µÇ¼ºó¸´ÖÆ

ÔÚЧÀÍÆ÷¶ËÆô¶¯Ö®ºó £¬ÎÒÃÇ¿ÉÒÔʹÓÃä¯ÀÀÆ÷·­¿ª¿Í»§¶ËÒ³Ã棨client.html£© £¬ÔÚÒ³ÃæÖÐÊäÈëÓû§ÃûºÍÃÜÂë £¬µã»÷µÇ¼°´Å¥ £¬Ð§ÀÍÆ÷¶Ë¾Í»áÊÕµ½µÇ¼ÇëÇó  ¡£µÇ¼Àֳɺó £¬ÎÒÃÇ¿ÉÒÔÔÚ̸Ìì¿òÖÐÊäÈë̸ÌìÐÂÎÅ £¬µã»÷·¢ËÍ°´Å¥ £¬Ð§ÀÍÆ÷¶Ë¾Í»á½«ÐÂÎÅת·¢¸øËùÓÐÔÚÏßÓû§  ¡£Í¨¹ýÕâ¸öÀý×Ó £¬ÎÒÃÇ¿ÉÒÔ¿´µ½Ê¹ÓÃSwooleʵÏÖHTML5ÓÎϷЧÀÍÆ÷µÄÀú³ÌºÜÊǼòÆÓ  ¡£

ËÄ¡¢×ܽá

HTML5ÊÖÒÕÕýÔÚÖð½¥³ÉΪÓÎÏ·¿ª·¢µÄÖ÷Á÷ £¬¶øSwoole×÷Ϊһ¿î¸ßÐÔÄܵÄÍøÂçͨѶ¿ò¼Ü £¬¿ÉÒÔΪHTML5ÓÎϷЧÀÍÆ÷µÄʵÏÖÌṩǿÓÐÁ¦µÄÖ§³Ö  ¡£±¾ÎÄÏÈÈÝÁËSwooleµÄ»ù±¾¿´·¨ºÍHTML5ÓÎϷЧÀÍÆ÷µÄ¼Ü¹¹ £¬Í¬Ê±Í¨¹ýÒ»¸ö¼òÆÓµÄʾÀýÑÝʾÁËÔõÑùʹÓÃSwooleʵÏÖHTML5ÓÎϷЧÀÍÆ÷  ¡£Ï£Íûͨ¹ý±¾ÎĵÄÏÈÈÝ £¬¶ÁÕßÄܹ»Ïàʶµ½ÔõÑùʹÓÃSwooleÀ´ÊµÏÖ¸ßÐÔÄܵÄHTML5ÓÎϷЧÀÍÆ÷  ¡£

ÒÔÉϾÍÊÇSwooleʵÏÖ¸ßÐÔÄܵÄHTML5ÓÎϷЧÀÍÆ÷µÄÏêϸÄÚÈÝ £¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí £¬°æȨÕùÒéÓë±¾Õ¾ÎÞ¹Ø £¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í尊龙凯时人生就是博ÂËÓÍ»úÍø¹Ù·½Ì¬¶È £¬Çë¶ÁÕß½ö×ö²Î¿¼  ¡£±¾ÎĽӴýתÔØ £¬×ªÔØÇë˵Ã÷À´ÓÉ  ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢ £¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢ £¬ÇëÄúÁ¬Ã¦ÁªÏµ尊龙凯时人生就是博ʵʱÐÞÕý»òɾ³ý  ¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ尊龙凯时人生就是博

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎå £¬9:30-18:30 £¬½ÚãåÈÕÐÝÏ¢

QR code
sitemap¡¢ÍøÕ¾µØͼ