public static void Main(string[] args) { //1) 创建连接 ConnectionConfig config = new ConnectionConfig(); config.Host = "127.0.0.1"; config.Port = 15555; BusClient client = new BusClient(config); //2) 异步控制结构 AsynCtrl ctrl = new AsynCtrl(); ctrl.Service = "MyService"; ctrl.PeerId = "local_mq"; //消息投递目标ID,与接收方协商 ctrl.MessageId = "10000"; ctrl.Timeout = 2500; ////异步投递失败提示最长时间(ms) //3) 消息体(帧组成) ZMsg msg = new ZMsg(); msg.PushBack("asyn call frame1"); msg.PushBack("asyn call frame2"); //4) 发送异步消息 msg = client.Send(ctrl, msg); msg.Dump(); //5)销毁链接 client.Destroy(); Console.ReadKey(); }
public static void Main(string[] args) { //1) 创建连接 ConnectionConfig config = new ConnectionConfig(); config.Host = "127.0.0.1"; config.Port = 15555; BusClient client = new BusClient(config); //2) 组装消息(消息帧数组) ZMsg request = new ZMsg(); request.PushBack("Frame1"); //消息帧1 -- 字符类型 request.PushBack(new byte[10]); //消息帧2 -- 二进制串 request.PushBack("request from C#"); //消息帧3 -- 字符类型 //3) 向ZBUS总线发送请求 ZMsg result = client.Request("MyService", "", request); result.Dump(); //4) 销毁客户端 client.Destroy(); Console.ReadKey(); }
public ZMsg HandleRequest(ZMsg request) { request.Dump(); ZMsg result = new ZMsg(); result.PushBack("this is from pooled c# worker"); return(result); }
/// <summary> /// Invoke remote procedure via zbus using JSON data format /// </summary> /// <param name="method">method name to invoke</param> /// <param name="args">method arguments</param> /// <returns>primitive type or Dictionary<string,object> instance</returns> public object Invoke(string method, params object[] args) { Dictionary <string, object> req = new Dictionary <string, object>(); req["id"] = "jsonrpc"; req["method"] = method; req["params"] = args; string json = JSON.Instance.ToJSON(req); ZMsg request = new ZMsg(); request.PushBack(this.Encoding.GetBytes(json)); ZMsg result = this.client.Request(this.Service, this.Token, request, this.timeout); if (result == null) { throw new ZBusException("json rpc request timeout"); } if (result.FrameSize != 2) { throw new ZBusException("json rpc result format error"); } Dictionary <string, object> res = null; string status = result.PopFrontStr(); if (status.Equals("200")) { try { json = result.PopBackStr(this.Encoding); res = (Dictionary <string, object>)JSON.Instance.Parse(json); if (res.ContainsKey("result")) { return(res["result"]); } else { throw new ZBusException((string)res["error"]); } } catch (System.Exception ex) { throw new ZBusException(ex.Message); } } else { throw new ZBusException(result.PopBackStr(this.Encoding)); } }
public ZMsg HandleRequest(ZMsg request) { ZMsg reply = new ZMsg(); reply.PushBack("200"); try { IEnumerator <byte[]> fe = request.GetEnumerator(); while (fe.MoveNext()) { string res = this.HandleJsonRequest(encoding.GetString(fe.Current)); reply.PushBack(encoding.GetBytes(res)); } } catch (System.Exception ex) { reply.Clear(); reply.PushBack("500"); string error = string.Format("Internal Error: {0}", ex.Message); reply.PushBack(encoding.GetBytes(error)); } return(reply); }
public static ZMsg ToZMsg(IntPtr msg) { ZMsg zmsg = new ZMsg(); IntPtr frame = C.zmsg_pop_front(msg); while (frame != IntPtr.Zero) { int frameSize = C.zmq_msg_size(frame); byte[] data = new byte[frameSize]; Marshal.Copy(C.zmq_msg_data(frame), data, 0, frameSize); zmsg.PushBack(data); C.zframe_destroy(out frame); frame = C.zmsg_pop_front(msg); } C.zmsg_destroy(out msg); return(zmsg); }
public static void Main(string[] args) { //1) 创建连接到ZBUS总线 ConnectionConfig connCfg = new ConnectionConfig(); connCfg.Host = "127.0.0.1"; connCfg.Port = 15555; Connection conn = new Connection(connCfg); //2) 注册服务(MyService) WorkerConfig workerCfg = new WorkerConfig(); workerCfg.Service = "MyService"; workerCfg.Mode = WorkerConfig.MODE_LB;//负载均衡模式 Worker worker = new Worker(conn, workerCfg); Console.WriteLine("C# Simple Worker Running..."); //3) 服务业务逻辑循环体(等待ZBUS总线分发请求,处理请求,返回请求结果) while (true) { try { //3.1) 等待ZBUS总线分发请求消息 ZMsg msg = worker.Recv(); //3.2) 业务逻辑处理请求消息 msg.Dump(); //3.3) 返回处理后的消息 msg = new ZMsg(); msg.PushBack("this is from c# loadbalancer"); worker.Send(msg); } catch (Exception e) { Console.WriteLine(e); break; } } //4) 清除连接相关 conn.Destroy(); worker.Destroy(); }
public static void Main(string[] args) { ConnectionConfig config = new ConnectionConfig(); config.Host = "127.0.0.1"; config.Port = 15555; //config.Verbose = true; BusClient client = new BusClient(config); ZMsg msg = new ZMsg(); msg.PushBack("publish from C#"); bool result = client.Publish("MyPubSub", "", "topic1", msg); Console.WriteLine("publish result: " + result); client.Destroy(); Console.ReadKey(); }