javascript 如何在 PHP 中创建 websockets 服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12020222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 15:05:20  来源:igfitidea点击:

How do I create a websockets server in PHP?

phpjavascriptwebsocketserver-sidehttp-protocols

提问by William Oliver

I have been searching the internet for about two days now, trying to understand how web sockets work, I am relatively new at Javascript and PHP, seeing as I started about two or three months ago.

我已经在互联网上搜索了大约两天,试图了解网络套接字的工作原理,我对 Javascript 和 PHP 相对较新,因为我大约在两三个月前开始使用。

I am extremely confused about what an Http protocol is, and how to perform a 'handshake' with a server as I have been reading about. So unfortunately it is difficult for me to ask a more specific question than the one in the title.

我对什么是 Http 协议以及如何与服务器执行“握手”感到非常困惑,因为我一直在阅读。所以不幸的是,我很难提出比标题中的问题更具体的问题。

However, even more than I would like an answer to the question in the title, I would really like a resource that will teach me about web sockets in a thorough way, such as a book or an online tutorial.

然而,比我想要标题中的问题的答案更重要的是,我真的想要一个能够以彻底的方式教我有关 Web 套接字的资源,例如一本书或在线教程。

Any help is appreciated, thanks.

任何帮助表示赞赏,谢谢。

回答by loganfsmyth

Sockets in general are just TCP streams that you can send data over. HTTP is built on top of them. WebSockets are build on/in parallel with HTTP, and allow for sending data back and forth to a browser in real-time. Standard sockets can do some of this, but browsers have tight security which makes it tough to use standard sockets to communicate with them.

套接字通常只是可以发送数据的 TCP 流。HTTP 建立在它们之上。WebSockets 建立在 HTTP 之上/与 HTTP 并行,并允许实时向浏览器来回发送数据。标准套接字可以做到这一点,但浏览器具有严格的安全性,这使得使用标准套接字与它们进行通信变得很困难。

WebSockets are useful when you need a persistent connection to the server from the browser, for real-time applications, or things like push notifications.

当您需要从浏览器到服务器的持久连接、实时应用程序或推送通知等时,WebSockets 非常有用。

I don't know much about PHP implementations unfortunately, but it looks like there is a library here: http://code.google.com/p/phpwebsocket/

不幸的是,我不太了解 PHP 实现,但看起来这里有一个库:http: //code.google.com/p/phpwebsocket/

Personally, I'd shy away from doing things like this in PHP because PHP/Apache can be pretty heavy, and since the socket is always open while someone is viewing the page, server resources can be used up pretty quickly.

就我个人而言,我会回避在 PHP 中做这样的事情,因为 PHP/Apache 可能非常繁重,而且因为当有人查看页面时套接字总是打开的,服务器资源可以很快用完。

A lot of people like to use NodeJS with socket.io because then you can use JavaScript on the server and the browser, but really it is up to preference. I would look at Socket.IO and find a language that has a good client for it.

很多人喜欢在 socket.io 中使用 NodeJS,因为这样你就可以在服务器和浏览器上使用 JavaScript,但实际上这取决于偏好。我会查看 Socket.IO 并找到一种具有良好客户端的语言。

It looks like this question might be useful too. Using PHP with Socket.io

看起来这个问题也可能有用。在 Socket.io 中使用 PHP

回答by starbeamrainbowlabs

Does this one fit the bill?

这个符合要求吗?

http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/

http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/

If not I can find another.

如果没有,我可以找到另一个。