/// <summary> /// 推入消息 /// </summary> /// <param name="body"></param> public PushResult Push(MessageBody body) { var result = new PushResult { PushStatus = PushStatus.Succeed }; try { _client.Send( "LindQueue_Push", SerializeMemoryHelper.SerializeToBinary(body), res => res.Buffer).ContinueWith(c => { if (c.IsFaulted) { throw c.Exception; } Console.WriteLine(SerializeMemoryHelper.DeserializeFromBinary(c.Result)); Logger.LoggerFactory.Instance.Logger_Debug("LindQueue_Push发送结果:" + Encoding.UTF8.GetString(c.Result)); }).Wait(); } catch (Exception ex) { result.PushStatus = PushStatus.Failed; result.ErrorMessage = ex.Message; } return(result); }
public void ExecuteCommand(FastSocket.SocketBase.IConnection connection, AsyncBinaryCommandInfo commandInfo) { if (commandInfo.Buffer == null || commandInfo.Buffer.Length == 0) { Console.WriteLine("BrokenPull参数为空"); connection.BeginDisconnect(); return; } var topic = SerializeMemoryHelper.DeserializeFromBinary(commandInfo.Buffer).ToString(); string topicQueueId = null; Action after = null; var offset = BrokerManager.GetConsumerQueueOffset(connection, topic, ref topicQueueId, ref after); var entity = BrokerManager.Pull(connection, topic, topicQueueId, offset); if (entity == null) { Console.WriteLine("服务端消息已被消费完!"); commandInfo.Reply(connection, null);//返回到客户端.. } else { if (after != null) { after(); } Console.WriteLine("向客户端返回消息对象:{0},IP:{1},Port:{2},connId:{3}", entity.ToString(), connection.RemoteEndPoint.Address.Address, connection.RemoteEndPoint.Port, connection.ConnectionID); commandInfo.Reply(connection, SerializeMemoryHelper.SerializeToBinary(entity));//返回到客户端.. } }
public void ExecuteCommand(FastSocket.SocketBase.IConnection connection, AsyncBinaryCommandInfo commandInfo) { if (commandInfo.Buffer == null || commandInfo.Buffer.Length == 0) { Console.WriteLine("BrokenPush参数为空"); connection.BeginDisconnect(); return; } var message = SerializeMemoryHelper.DeserializeFromBinary(commandInfo.Buffer) as MessageBody; try { BrokerManager.Push(message); string result = string.Format("消息成功加入队列,Topic:{0},QueueId:{1},QueueCount:{2}", message.Topic, message.QueueId, message.QueueOffset); Console.WriteLine(result); commandInfo.Reply(connection, SerializeMemoryHelper.SerializeToBinary("OK"));//返回到客户端.. } catch (Exception) { throw; } }
/// <summary> /// 消费者开始消费消息 /// 单线程消费,减少并发冲突 /// </summary> public void Start() { while (true) { foreach (var item in _allConsumers) { foreach (var key in item.Callback.Keys) { item.Client.Send("LindQueue_Pull", SerializeMemoryHelper.SerializeToBinary(key), res => res.Buffer).ContinueWith(c => { if (c.IsFaulted) { throw c.Exception; } if (c.Result == null) { Console.WriteLine("指定topic队列为空,服务挂起15秒!"); Thread.Sleep(15000); } else { var entity = SerializeMemoryHelper.DeserializeFromBinary(c.Result) as MessageBody; item.Callback[key](entity); } }).Wait(); } } } }
public void Insert(TEntity item) { if (item != null) { _db.HashSet(tableName, item.Id, SerializeMemoryHelper.SerializeToBinary(item)); } }
public void Update(TEntity item) { if (item != null) { var old = Find(item.Id); if (old != null) { _db.HashDelete(tableName, item.Id); _db.HashSet(tableName, item.Id, SerializeMemoryHelper.SerializeToBinary(item)); } } }
/// <summary> /// 从WEB页面发FastSocket请求 /// </summary> /// <returns></returns> public ActionResult Index() { //注册服务器节点,这里可注册多个(name不能重复) var client = new DSSBinarySocketClient(8192, 8192, 3000, 3000); client.RegisterServerNode("127.0.0.1:8403", new System.Net.IPEndPoint(System.Net.IPAddress.Parse("127.0.0.1"), 8403)); client.Send("UserInsert", 1, "zzl", 1, "test", SerializeMemoryHelper.SerializeToBinary("hello web world!"), res => res.Buffer).ContinueWith(c => { if (c.IsFaulted) { throw c.Exception; } Console.WriteLine(Encoding.UTF8.GetString(c.Result)); }); ViewBag.msg = "消息发送了"; return(View()); }
public void ExecuteCommand(IConnection connection, AsyncBinaryCommandInfo commandInfo) { if (commandInfo.Buffer == null || commandInfo.Buffer.Length == 0) { Console.WriteLine("SendQueue参数为空"); connection.BeginDisconnect(); return; } var entity = SerializeMemoryHelper.DeserializeFromBinary(commandInfo.Buffer); string str = "result:1,CmdName:" + commandInfo.CmdName + ",SeqID:" + commandInfo.SeqID + ",message:" + entity.ToString(); Console.WriteLine(str); var old = new User { Name = "hello world" }; commandInfo.Reply(connection, SerializeMemoryHelper.SerializeToBinary(old));//返回到客户端.. }