简体   繁体   English

将WebSockets支持添加到本地Web服务器

[英]Adding WebSockets support to local web server

I'm developing a multiplayer Javascript/canvas game that uses WebSockets to communicate player positions and states etc. I'm comfortable coding this in Javascript, but it's the server-side websockets stuff that I'm having trouble with. 我正在开发一个多人Javascript /画布游戏,它使用WebSockets来传达玩家位置和状态等。我很乐意用Javascript编写这个,但它是我遇到麻烦的服务器端websockets东西。

I have no idea how I should implement WebSocket support past the client side Javascript. 我不知道如何通过客户端Javascript实现WebSocket支持。 Do I add a module to Apache? 我是否向Apache添加了一个模块? Do I do it in PHP? 我是用PHP做的吗? Do I use something like node.js? 我是否使用像node.js这样的东西? As far as I know node.js has received a lot of praise about this sort of stuff, but I still have no idea what it really is other than a web server. 据我所知node.js已经收到了很多关于这类东西的赞誉,但我还是不知道除了Web服务器之外它究竟是什么。

I would recommend using nodejs, along with socket.io , which is a cross-browser compatible web sockets style library. 我建议使用nodejs以及socket.io ,它是一个跨浏览器兼容的Web套接字样式库。 It makes it really simple to write code which communicates with the server (==nodejs) in real time. 它使编写与服务器(== nodejs)实时通信的代码变得非常简单。 It uses web sockets in browsers which support them, or other technologies in browsers which don't. 它在支持它们的浏览器中使用Web套接字,或者在浏览器中使用其他技术。

Just a note regarding PHP since you mentioned it... It is not very suitable for this in my opinion, since, as you may know, a PHP script only lives the duration of the request. 关于PHP的一个注释,因为你提到它...在我看来它并不适合这个,因为你可能知道,PHP脚本只存在请求的持续时间。 This behavior makes it difficult to implement real time communication, unless you roll your own server with PHP, skipping Apache or such entirely. 这种行为使得实现实时通信变得困难,除非您使用PHP滚动自己的服务器,完全跳过Apache等。

Another alternative could be using Python, and perhaps something like Twisted , although you may need to implement websockets or such yourself. 另一个替代方案可能是使用Python,也许像Twisted一样,尽管你可能需要自己实现websockets等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM