Created
March 31, 2014 09:28
-
-
Save mafintosh/9888679 to your computer and use it in GitHub Desktop.
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.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Net.Sockets; | |
namespace RedisPublisher | |
{ | |
class Publisher | |
{ | |
private TcpClient _client = null; | |
private NetworkStream _stream = null; | |
private string _pass = null; | |
private string _host = null; | |
private int _port = 0; | |
public Publisher(int port, string host, string pass = null) | |
{ | |
_port = port; | |
_host = host; | |
_pass = pass; | |
} | |
public void Publish(string name, string data) | |
{ | |
Connect(); | |
Write( | |
"*3\r\n" + | |
"$7\r\n" + | |
"publish\r\n" + | |
"$" + name.Length + "\r\n" + | |
name + "\r\n" + | |
"$" + data.Length + "\r\n" + | |
data + "\r\n" | |
); | |
} | |
private void Connect() | |
{ | |
if (_client != null) return; | |
_client = new TcpClient(_host, _port); | |
_stream = _client.GetStream(); | |
if (_pass == null) return; | |
Write( | |
"*2\r\n"+ | |
"$4\r\n"+ | |
"auth\r\n"+ | |
"$"+_pass.Length+"\r\n"+ | |
_pass+"\r\n" | |
); | |
} | |
private void Write(string message) { | |
Byte[] data = System.Text.Encoding.ASCII.GetBytes(message); | |
_stream.Write(data, 0, data.Length); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var port = ...; | |
var p = new Publisher(port, "... hostname ...", "... password ..."); | |
p.Publish("my-channel:dotnet", "[{\"hello\":\"world\"}]"); | |
p.Publish("my-channel:dotnet", "[{\"ny\":\"test\"}]"); | |
Console.ReadLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment