javascript Omegle 是如何运作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7228403/
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
How does Omegle work?
提问by mowwwalker
Sorry for the vague question, but Omegle has exactly what I want for my site. It's the bare chat system. You send and receive messages instantly. I've spent today looking for a shoutbox method, but all I could find were people saying to use MySQL and javascript intervals to keep checking. Omegle updates instantly with new messages though. How does it all work?
抱歉这个含糊不清的问题,但 Omegle 正是我想要的网站。这是裸聊系统。您可以立即发送和接收消息。我今天一直在寻找一种shoutbox方法,但我能找到的只是人们说要使用MySQL和javascript间隔来继续检查。不过,Omegle 会立即使用新消息更新。它是怎么运行的?
回答by Gabi Purcaru
You should read this wikipedia article, it will give you a better insight of the process.
你应该阅读这篇维基百科文章,它会让你更好地了解这个过程。
The main keywords here are long-polling, websockets, flash sockets.
这里的主要关键字是长轮询、websockets、flash sockets。
Also, this is (one of the places) where node.js+socket.ioshine.
回答by Cipi
Its a piece of cake. All you need is server that you can write in a couple of minutes (in C#, not sure about PHP). Server should accept HTTP GET request, like this for example:
很简单的。您所需要的只是可以在几分钟内编写的服务器(使用 C#,不确定 PHP)。服务器应该接受 HTTP GET 请求,例如:
GET /chat?room=someroom&me=Jerod HTTP/1.1
GET /chat?room=someroom&me=Jerod HTTP/1.1
and keep its connection open (just accept TcpClient
object and sore it under some dictionary for example Dictionaty<string, Dictionary<string, TcpClient>> clients;
like this: clients["someroom"].Add("Jerod", tcpClient);
and hold it until there is data to send back to Jerod)
并保持其连接打开(只需接受TcpClient
对象并将其放在某个字典下,例如Dictionaty<string, Dictionary<string, TcpClient>> clients;
像这样:clients["someroom"].Add("Jerod", tcpClient);
并保持它直到有数据发送回 Jerod)
And it should accept POST, which holds text the user is trying to send to other users in room:
它应该接受 POST,其中包含用户尝试发送给房间中其他用户的文本:
POST /chat?room=someroom&me=Sara HTTP/1.1
Content-Length: 2
Hi
When the server gets this, it knows that Sara is sending "Hi" to all users in room "someroom". Since Jerod's, GET never got answered, connection is still open, and all server has to do is get that connection from a dictionary (or some other data structure) and respond with:
当服务器收到此信息时,它知道 Sara 正在向房间“someroom”中的所有用户发送“Hi”。由于 Jerod 的 GET 从未得到答复,连接仍然打开,所有服务器要做的就是从字典(或其他一些数据结构)中获取该连接并响应:
HTTP 200 OK
Content-Length: <length>
Sara: Hi
And, on clients side, you would have one GET XMLHttpRequest object (AJAX with GET method) that will have its timeout set to 0 (block to infinity) and it will be used to open a connection to server through which it will forward stuff that other people are sending.
并且,在客户端,您将有一个 GET XMLHttpRequest 对象(带有 GET 方法的 AJAX),它将其超时设置为 0(块为无穷大),它将用于打开与服务器的连接,通过该连接它将转发以下内容其他人正在发送。
And you need one more POST XMLHttpRequest which you will use to send data to other clients.
并且您还需要一个 POST XMLHttpRequest 用于向其他客户端发送数据。
When first AJAX (GET) succeeds, you write out the TEXT that it holds (it is "Sara: Hi") to some DIV, and make the same GET request again so the server can let the user know when someone else has posted after Sara...
当第一个 AJAX (GET) 成功时,您将它保存的 TEXT(它是“Sara: Hi”)写出到某个 DIV,然后再次发出相同的 GET 请求,以便服务器可以让用户知道其他人何时发布了萨拉...
When second AJAX (POST) succeeds, you write to DIV what ever the user has entered to be sent, prepended with username, like so in JS: document.getElementById("chat").innerHTML += uname+": "+txt;
当第二个 AJAX (POST) 成功时,您将用户输入的内容写入 DIV,并在前面加上用户名,就像在 JS 中一样: document.getElementById("chat").innerHTML += uname+": "+txt;
Its pretty straight forward and simple.
它非常直接和简单。