Click here to Skip to main content
15,895,777 members
Articles / Programming Languages / C#

Omegle4COM Third Party Library

Rate me:
Please Sign up or sign in to vote.
4.67/5 (2 votes)
2 Dec 2013CPOL7 min read 17.7K   2   3
Omegle4COM .NET Omegle Interface

Additional Terms 

Any derivative  or non-derivative distribution of this library must include credit to the original author.   

Background

Omegle is a website made by Omegle(LLC) in which you can connect to random strangers and chat with them, I have made a library for interfacing this website through a series of http requests. I have browsed through and found a couple examples on how to do this in python which were helpful in reverse engineering the website, but no useful ones in C# or any .NET compatible language for that  matter, so I decided to make my own. I am in no way, shape, or form affiliated with Omegle(LLC) , I simply made this library because I saw a demand for it.  

Introduction

This article will describe the use of and distribute the Omegle4COM DLL. This is not a tutorial on how to use dll's in general, it is simply a reference for the use of the Omegle4COM DLL. This DLL will allow you to connect to Omegle and interface with it so that you may connect to and communicate with other Omegle users. It features everything from sending and receiving messages to showing that your typing or connecting with common interests, almost anything that can be achieved on the original site can be achieved programatically through this library. 

Required Knowledge 

Should have a basic understanding of programming in your respective .NET compatable programming language.

Should know how to register events.

Should know how to interface with external libraries. 

Using the code

Assuming you have already referenced the library in your project (by the way, the only required library reference is the Omegle4COM.dll, however you will need both Omegle4COM.dll and Newtonsoft.json.dll in the assembly directory for the application to work correctly), the first step is to import the Omegle4COM Namespace. After you have done so, make a new Omegle object instance. At this point we are going to register some events, there are multiple events but were going to use three, StrangerConnectedMessageReceived, and StrangerDisconnected. The event handlers are going to look something like this:

C#
public void omegle_MessageReceived(Object sender, String data) 
<span style="font-size: 9pt;">{
	String msg = data; //the only time data contains anything useful
	//Is durring the MessageReceived event
</span><span style="font-size: 9pt;">}</span><span style="font-size: 9pt;"</span> 

NOTE: The StrangerDisconnected event is called when either you or the stranger disconnects. Now to start a chat with a new random stranger. To do this, we are going to call the Omegle.Connect() the connect function accepts two parameters and one optional parameter. The first parameter is a boolean, this determines if it will wait for a stranger to connect before calling the ResultAttachment(String) delegate and moving on. The second parameter is an array of strings, these will be the topics or interests that the Omegle site looks for when trying to connect you to a stranger, if there are no values in the array, it will find you a completely random stranger. The last argument is optional, it is a delegate method that accepts a String parameter, this is currently pointless as the only thing it does is pass you the conversation ID, and I have not implemented that in any useful way yet, but in the future I plan on adding a static object method, which will accept the id so that there is no need for an object reference.  At this point your probably going to want to communicate with the stranger, so first we'll show them were typing by calling Omegle.ShowTyping() then when were done typing were going to call Omegle.StopTyping(). Now to send the message were going call Omegle.SendMessage("This is a message"). Then were simply going to disconnect, Omegle.Disconnect(). And that's it! Now your a step closer to making your own Omegle integrated program, have fun! 

Methods, Variables, and Events 

Name  Type Parameters Description 
Omegle  Class/Type Main class instance 
OmegleEventHandler Delegate Object, String Event Delegate 
StrangerConnected OmegleEventHandler Object, String Called when you are connected to a stranger 
StrangerTyping OmegleEventHandler Object, String Called when the stranger begins typing 
StrangerStoppedTyping OmegleEventHandler Object, String Called when the stranger stops typing 
MessageReceived OmegleEventHandler Object, String Called when the stranger sends you a message, passes the message through the string parameter 
StrangerDisconneced OmegleEventHandler Object, String Called when you or the stranger disconnects 
connected bool Determines  if you are connected to a stranger 
Connect void bool, String[], Action<String> = null Attaches you to Omegle and begins search for stranger with topics in common with you, will wait for stranger to connect if bool is true. ResultAttachment is called when action is complete 
ShowTyping void Shows stranger that you are typing 
StopTyping void Shows stranger that you have stopped typing 
SendMessage   void  String Sends a message to the stranger  
Disconnect void Disconnects from the stranger 

Example

This is a very crude example using the C# Console Application, it is a turn by turn based Omegle desktop client. 

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
//Include the dll
using Omegle4COM;

namespace Omegle_Desktop
{
    class Program
    {
        static Object objLock = new Object();
        static bool run = true;
        static Omegle omegle = new Omegle();

        static void Write(string value) { lock (objLock) { System.Console.Write(value); } }

        static void WriteLine(string value) { lock (objLock) { System.Console.WriteLine(value); } }

        static string ReadLine() { lock (objLock) { return System.Console.ReadLine(); } }

        static void Main(string[] args)
        {
            //Register events
            omegle.MessageReceived += new Omegle.OmegleEventHandler(onMessage);
            omegle.StrangerTyping += new Omegle.OmegleEventHandler(onTyping);
            omegle.StrangerStoppedTyping += new Omegle.OmegleEventHandler(onStoppedTyping);
            omegle.StrangerDisconnected += new Omegle.OmegleEventHandler(onDisconnect);

            WriteLine("Searching For Stranger!");
            //Connect to omegle, interests being the command line args
            //It will wait for someone to connect before continuing
            omegle.Connect(true, args);
            WriteLine("Found One!");
            Write(">");
            //Show them that were typing
            omegle.ShowTyping();
            String msg = ReadLine();
            //Show them we stopped typing
            omegle.StopTyping();
            WriteLine("You: " + msg);
            //Send the message
            omegle.SendMessage(msg);
            //wait for stranger to disconnect
            while (run)
            {
            }
            WriteLine("Stranger Disconnected");
            Console.ReadKey(true);
        }

        static void onMessage(Object sender, String data)
        {
            //Print the message
            WriteLine("Stranger: " + data);
            Write(">");
            //Show stranger were typing
            omegle.ShowTyping();
            String msg = ReadLine();
            //Show stranger weve stopped typing
            omegle.StopTyping();
            WriteLine("You: " + msg);
            //Send message
            omegle.SendMessage(msg);
        }

        static void onTyping(Object sender, String data)
        {
            WriteLine("Stranger Typing...");
        }

        static void onStoppedTyping(Object sender, String data)
        {
            WriteLine("Stranger Stopped Typing");
        }

        static void onDisconnect(Object sender, String data)
        {
            //Stop program
            run = false;
        }
    }
}

How The Library Works

In this section I will be going in depth on how my library works and how to possibly make your own. To do this you must first understand how the Omegle website works. At first I thought it worked through some form of JavaScript, and while I was partially correct, it relies more heavily on a series of http(POST/GET) requests. First it selects one of many fronts to operate off of, I haven't quite figured out how it determines which front to use, so I usually use front7. The front is the website gateway in which it decides to use when querying the website, the front is appended to the website in the form of a subdomain, like so: http://front#.omegle.com/ where # is replaced with the front number. after it has done this, it calls the start page with a get request. The there are multiple variables it passes, but we only care about two, topics and lang. The topics are the interests the website uses when trying to connect you to a partner, and is submitted in the form of a JSON array. The lang is the language of which we are operating on, for our purposes, I use en. So our final request, will look something like this:

http://front7.omegle.com/start?topics=["topic1","topic2","topic3","etc..."]&lang=en<span style="color: rgb(17, 17, 17); font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px;"> </span> 

Now that request will return the session ID, we'll need that to send messages and listen to events. The ID is returned wrapped in quotation marks, so you might want to do something like this (C#):

C#
String ID;

public void Start()
{
	WebClient web = new WebClient();
	Using(StreamReader sr = new StreamReader(web.OpenUrl("<span style="font-size: 9pt;">http://front7.omegle.com/start?topics=["topic1","topic2","topic3","etc..."]&lang=en</span><span style="font-size: 9pt;">")))
		data = sr.ReadToEnd();
	ID = ID.Remove(0, 1);</span> 
	ID = ID.Remove(ID.Length - 1, 1);
<span style="font-size: 9pt;">} </span>

 In the above code, we create a WebClient object instance. Then we use this client to open a stream to the url, and read the stream into a string called ID. We then proceed to remove the first and last characters in the  string, leaving us with the isolated ID. At this point we have only initialized a session, and begun looking for a stranger, we haven't actually connected to a stranger yet. Now that we've initiated a new session, we need to check for events.  Now to do this were going to have to send a POST request to front#.omegle.com/events containing one variable, id. Since we are sending a post request and not a GET request, were going to have to approach things a little differently. We're going to make a new String method called GetPostResponse, this will query the server and return the response, this method will accept the ID. 

C#
private string GetPostResponse(String str)
        {
            try
            {
                WebRequest req = WebRequest.Create("http://front7.omegle.com/events");
                WebResponse resp;
                req.ContentType = "application/x-www-form-urlencoded";
                req.Method = "POST";
                Byte[] Bytes = System.Text.ASCIIEncoding.ASCII.GetBytes("id=" + str);
                req.ContentLength = Bytes.Length;
                using (Stream stream = req.GetRequestStream())
                    stream.Write(Bytes, 0, Bytes.Length);
                resp = req.GetResponse();
                using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
                    return sr.ReadToEnd();
            }
            catch (Exception)
            {
                return ("null");
            }

        } 

 Now you can try to look through that method and figure out what all that means, but I'm just going to tell you, that you give it the ID, and it will return the event response. Now all you need to do, is figure out what event is being called, we do that with a simple if String.Contains() statement, so like this:

C#
String data = <span style="font-size: 9pt;">GetPostResponse(ID);</span>

if(data.Contains("connected"))
{
	//do something

 The events are:

gotMessage
connected
typing
<span style="font-size: 9pt;">stoppedTyping
</span>strangerDisconnected

I would suggest checking for gotMessage first, just in case the message contains one or more of the event keywords. To obtain the message from gotMessage, you have to use JSON.NET. After that it is fairly simple, import Newtonsoft.json.Linq then use this little line of code to obtain the message:

C#
String msg = (String)JArray.Parse(data)[0][1]; 

 Basically, this parses the JSON string into usable data, then obtains the value at 0 and then the subvalue at 1.  Now we are probably going to want to send data to the stranger, so we are just going to do this:

C#
 url.UploadValues("http://front7.omegle.com/send", new NameValueCollection() { { "msg", mesg }, { "id", ID } });  

That one would send a message to the  stranger, where mesg is the message and ID is the ID. You can also show the user that you are typing or have stopped typing with front#.omegle.com/typing and front#.omegle.com/stoppedTyping  but you would have to remove the msg variable, so remove the entire:

C#
{"msg", mesg},

from the query. Now all there is left to do, is disconnect, you do it the exact same way as showing your typing, except for you will direct the query towards front#.omegle.com/disconnect . And thats it! 

History    

1.0.0 - Release

***DISCLAIMER*** 

I am in no way shape or form affiliated with JSON.NET or Omegle (LLC), this library is completely third party, use at your own risk. Make sure to read all of the terms and conditions associated with the Omegle  website before using this lib.  

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO CalcuProcessing
United States United States
I am a 16 year old programmer/engineer/producer. I have been into computers since I was 9, I was brought into programming through qBasic and expanded upon it with VB.net, vb remains one of my favorite languages to program in however when doing professional things such as making games and business resources I use c++/java. I also design websites and am into graphic design a little bit, just a side thing I hope to one day do for money(my music production is in the same boat).

Comments and Discussions

 
QuestionWhy Aren't You Out Chasing Girls? Pin
thund3rstruck2-Dec-13 10:28
thund3rstruck2-Dec-13 10:28 
AnswerRe: Why Aren't You Out Chasing Girls? Pin
Sicppy2-Dec-13 15:15
Sicppy2-Dec-13 15:15 
QuestionCode Formatting Error Pin
Sicppy2-Dec-13 4:55
Sicppy2-Dec-13 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.