websocket php vs node js

ste*_*143 5 javascript php websocket node.js

有人可以说“websocket php” http://www.php.net/manual/en/sockets.examples.php > 和 node.js之间有什么区别?. 我使用 websocket php 聊天,但我不知道这个聊天是否会转移到 node.js 会更好?

小智 1

Websockets 是基于 TCP 套接字的传输。如果您在提供的链接中注意到,作者建议手动解码数据帧。正如 @oberstet 推荐的那样,有些项目可以帮助您解决此问题(另请参阅https://github.com/nicokaiser/php-websocket)。@Kanaka 对 websockets 和原始 TCP 套接字之间的区别有很好的解释。

使用 Node.js 无疑是一种智能、低开销的方式来部署 WebSocket 连接服务器。另一种选择是使用实时网络。PubNub 特别有一篇关于如何用 10 行代码编写聊天应用程序的博客文章,该文章非常容易理解。简而言之,代码是:

Enter Chat and press enter
<div><input id=input placeholder=you-chat-here /></div>

Chat Output
<div id=box></div>

<script src=http://cdn.pubnub.com/pubnub.min.js></script>
<script>(function(){
    var box = PUBNUB.$('box'), input = PUBNUB.$('input'), channel = 'chat';
        PUBNUB.subscribe({
            channel : channel,
            callback : function(text) { box.innerHTML = (''+text).replace( /[<>]/g, '' ) + '<br>' + box.innerHTML }
        });
        PUBNUB.bind( 'keyup', input, function(e) {
            (e.keyCode || e.charCode) === 13 && PUBNUB.publish({
            channel : channel, message : input.value, x : (input.value='')
        })
    } )
})()</script>
Run Code Online (Sandbox Code Playgroud)

像这样的简单方法将允许您完全删除服务器托管和配置。希望这可以帮助!