Created
October 27, 2012 04:32
-
-
Save wizzard0/3962949 to your computer and use it in GitHub Desktop.
Microchat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using Newtonsoft.Json; | |
using System.Security.Cryptography.X509Certificates; | |
namespace Chat | |
{ | |
class Program | |
{ | |
private static bool RemoteCertificateValidate(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error) | |
{ | |
if ((sender is HttpWebRequest) && ((HttpWebRequest)sender).Host == "c3.localdomain:6984") | |
if (error != System.Net.Security.SslPolicyErrors.None || !cert.Subject.Contains("O=Oleksandr Nikitin") || !cert.Subject.Contains("c3.localdomain")) return false; | |
return true; | |
} | |
static void Main(string[] args) | |
{ | |
ServicePointManager.ServerCertificateValidationCallback += RemoteCertificateValidate; | |
string chan = Console.ReadLine(); | |
int u = JsonConvert.DeserializeObject<HistoryInfo>((new WebClient().DownloadString("https://c3.localdomain:6984/chat/"))).update_seq; | |
(new System.Threading.Thread(() => { | |
while (u++ != 0) | |
{ | |
var changes = (new WebClient()).DownloadString("https://c3.localdomain:6984/chat/_changes?feed=longpoll&filter=router/do&Channel=" + chan + "&include_docs=true&since=" + u); | |
foreach (var x in JsonConvert.DeserializeObject<ResultsList>(changes).results) Console.WriteLine(x.doc.DataString); | |
} | |
})).Start(); | |
while (true) | |
{ | |
var em = new ChatMessage() { DataString = Console.ReadLine(), To = chan }; | |
var wc = new WebClient(); | |
wc.Headers["Content-Type"] = "application/json"; | |
wc.UploadString("https://c3.localdomain:6984/chat", JsonConvert.SerializeObject(em)); | |
} | |
} | |
} | |
public class HistoryInfo { public int update_seq; } | |
public class Result { public ChatMessage doc; } | |
public class ResultsList | |
{ | |
public System.Collections.Generic.List<Result> results; | |
public int last_seq; | |
} | |
public class ChatMessage | |
{ | |
public string To; | |
public string DataString; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// server installation: | |
// 1. install CouchDB http://couchdb.apache.org/#download | |
// 2. curl -v -H "Content-Type: application/json" -X PUT --data "server.json" https://c3.localdomain:6984/chat | |
{ | |
"_id": "_design/router", | |
"filters": { | |
"do": "function(doc, req) { if(req.query.Channel.split(',').indexOf(doc.To)!=-1) { return true; } else { return false; }}" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment