开发者

How does Omegle work?

开发者 https://www.devze.com 2023-03-31 23:54 出处:网络
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 shoutb

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?


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.

Also, this is (one of the places) where node.js+socket.io shine.


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:

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)

And it should accept POST, which holds text the user is trying to send to other users in room:

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:

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.

And you need one more POST XMLHttpRequest which you will use to send data to other clients.

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...

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;

Its pretty straight forward and simple.


I'd suggest that you learn Node.js and Socket.io. Socket.io is a node.js library that basically allows you to create a real-time environment where you can send instant messages on a webpage. Barely takes a day to learn Socket.io if you're already proficient in JavaScript and Node.js. Socket.io basically just connects two or more people to a single webpage at the same time. So when any activity takes place on the page, everyone connected can see the activity. So when you post a message on the webpage, everyone connected can see it. So once you've established this type of connection, you create a chat box on the webpage then when a person connected to the page submits a message, it just creates a

element in JavaScript then make the InnerHTML of that

element the text in the chat box. Then finally just append that

element to the page. Done! You've successfully created an instant message feature on your webpage.

0

精彩评论

暂无评论...
验证码 换一张
取 消