最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I create a websockets server in PHP? - Stack Overflow

programmeradmin3浏览0评论

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.

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.

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.

Any help is appreciated, thanks.

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.

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.

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.

Any help is appreciated, thanks.

Share Improve this question asked Aug 18, 2012 at 16:24 William OliverWilliam Oliver 1,4324 gold badges25 silver badges36 bronze badges 3
  • 1 Reference php/manual/en/book.sockets.php – Jared Drake Commented Aug 18, 2012 at 16:27
  • 1 @JaredDrake Sockets and websockets are related but very different beasts. – loganfsmyth Commented Aug 18, 2012 at 16:35
  • @loganfsmyth, could you elaborate on that? – William Oliver Commented Aug 18, 2012 at 16:46
Add a ment  | 

2 Answers 2

Reset to default 11

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 municate with them.

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

I don't know much about PHP implementations unfortunately, but it looks like there is a library here: http://code.google./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.

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.

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

Does this one fit the bill?

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

If not I can find another.

发布评论

评论列表(0)

  1. 暂无评论