/* * The function occurs every time the event(event handler in CustomQueue toSend) happens. * every time you wants to send message you call addTosend function from client class that call this function by event handler */ private void HandleMessages(object sender, dynamic requestVar) { if (toSend.Count > 0) { ClientMSG _msg = null; bool isSuccessful = toSend.TryDequeue(out _msg);//save in _msg if (isSuccessful) { //send message to server requestVar.Send(new ZFrame(JsonConvert.SerializeObject(_msg))); using (ZFrame reply = requestVar.ReceiveFrame()) { string recived = reply.ReadString(); dynamic serverMsg = DeserializeServerMsg(recived);// we can't know whats the object type if (serverMsg != null) { ///111 = serverEndCallPubSub if (serverMsg.Type == "111" && callThread != null) { callThread.Abort();// stop subscribing to call topic callThread = null; } Message.Invoke(this, serverMsg);// an event that does changes in the form } } } } }
/* * this function add new message to CustomQueue. * (by the event handler you call HandleMessages function ) */ public void addTosend(ClientMSG msg) { toSend.Enqueue(msg); toSend.Notify(requester); }