📢 C# Winform实现TibcoRV中间件通信项目
🌟 前言
TIBCO Rendezvous(TIBCO RV)是一款高性能的中间件,专为实时消息传递设计,适用于对延迟极度敏感的场景。
本文将用C# Winform实现TIBCO RV的通信功能,实现连接、侦听、发送与接收全流程,文中附上完整源码及安装测试步骤。
🔍核心概念速览
发布/订阅模型:基于主题的消息路由机制
对等网络架构:无中心节点,分布式通信
实时性能:低延迟、高吞吐量
核心组件:Daemon(rvrd)、Subject(主题)、Transport(通信端点)、Listener(侦听器)
🛠️ 环境准备
TIBCO RV版本:8.4
开发环境:Windows 11
开发工具:Visual Studio 2022 + .NET Framework 4.5.2
安装包:Tibrv8.4 win32 + win64
Tibrv8.4.zip链接: https://pan.baidu.com/s/14pTyLtHg8il1z_0cwz_-kw?pwd=6666
🖥️ 案例演示
💻 核心代码片段
1. 连接与断开逻辑
private void btn_Connect_Click(object sender, EventArgs e)
{
bool isStartConnect = btn_Connect.Text.Equals("连接");
if (isStartConnect)
{
tibcoRVTHelper = new TibcoRVTHelper(tbx_Server.Text, tbx_Network.Text, tbx_Daemon.Text, tbx_ListenSubject.Text, tbx_TargetSubject.Text);
tibcoRVTHelper.StartConnect();
}
else
{
tibcoRVTHelper.DisConnected();
}
}
2. 消息接收与显示
public void OnMessageReceived(object sender, MessageReceivedEventArgs e)
{
TIBCO.Rendezvous.Message message = e.Message;
string receiveData = message.GetFieldByIndex(0);
rtbx_ReceiveData.AppendText($"{
DateTime.Now}】{
receiveData}\n");
}
💻 完整代码
Form界面类主要代码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using TIBCO.Rendezvous;
namespace Demo_TibcoRV
{
public partial class Frm_TibcoRV : Form
{
TibcoRVTHelper tibcoRVTHelper;
public Frm_TibcoRV()
{
InitializeComponent();
this.MinimumSize = new System.Drawing.Size(750, 650);
}
private void Frm_TibcoRV_Load(object sender, EventArgs e)
{
ControlStyleUpdata( picBx_ConnectStatu, Color.Gray);
ControlStyleUpdata(picBx_ListenStatu, Color.Gray);
btn_ListenOpen.Enabled = false;
}
#region 事件
/// <summary>
/// 连接按钮
/// </summary>
private void btn_Connect_Click(object sender, EventArgs e)
{
bool isStartConnect = btn_Connect.Text.Equals("连接")?true:false;
//开始连接
if (isStartConnect)
{
tibcoRVTHelper = new TibcoRVTHelper(tbx_Server.Text, tbx_Network.Text, tbx_Daemon.Text,tbx_ListenSubject.Text,tbx_TargetSubject.Text);
tibcoRVTHelper.MessageField = tbx_MessageField.Text;
tibcoRVTHelper.messageReceivedHandler += OnMessageReceived;
tibcoRVTHelper.ListenedStatusHandler += OnListened;
tibcoRVTHelper.ConnectedStatusHandler += OnConnected;
tibcoRVTHelper.ErrorMessageHandler += TibcoRVTHelper_ErrorMessageHandler;
tibcoRVTHelper.StartConnect();
}
//断开连接
else
{
tibcoRVTHelper.DisConnected();
ControlStyleUpdata(picBx_ConnectStatu, Color.Gray);
ControlStyleUpdata(picBx_ListenStatu, Color.Gray);
btn_Connect.Text = tibcoRVTHelper.IsConnected ? "断开" : "连接";
ControlUpdata(true);
}
}
private void TibcoRVTHelper_ErrorMessageHandler(object sender, string message)
{
MessageShow(message);
}
/// <summary>
/// 侦听按钮
/// </summary>
private void btn_ListenOpen_Click(object sender, EventArgs e)
{
//正在连接,可以侦听
if (tibcoRVTHelper.IsConnected)
{
tibcoRVTHelper.TargetSubject = tbx_TargetSubject.Text;
tibcoRVTHelper.ListenSubject = tbx_ListenSubject.Text;
}
//未连接,不允许侦听
else
{
tibcoRVTHelper.messageReceivedHandler -= OnMessageReceived;
}
btn_ListenOpen.Text = tibcoRVTHelper.IsListened ? "停止" : "侦听";
ControlStyleUpdata(picBx_ListenStatu, tibcoRVTHelper.IsListened? Color.LimeGreen: Color.Gray);
}
/// <summary>
/// 发送按钮
/// </summary>
private void btn_SendData_Click(object sender, EventArgs e)
{
if (tibcoRVTHelper.IsConnected)
{
//tibcoRVTHelper.MessageField = "xmlData";
tibcoRVTHelper.Send(rtbx_SendData.Text);
}
}
/// <summary>
/// 客户端、服务端
/// </summary>
private void checkBox_IsClient_CheckedChanged(object sender, EventArgs e)
{
if (tibcoRVTHelper == null ||!tibcoRVTHelper.IsConnected)
{
string temp = tbx_ListenSubject.Text;
tbx_ListenSubject.Text = tbx_TargetSubject.Text;
tbx_TargetSubject.Text = temp;
}
}
#endregion
#region 委托方法
/// <summary>
/// 委托方法:消息接收
/// </summary>
public void OnMessageReceived(object sender, MessageReceivedEventArgs messageReceivedEventArgs)
{
TIBCO.Rendezvous.Message message = messageReceivedEventArgs.Message;
string receiveData = message.GetFieldByIndex(0);
string fieldName = message.GetFieldByIndex(0).Name;
MessageShow($"send subject = {
message.SendSubject}\r\n field name = {
fieldName}\r\n{
receiveData}");
}
/// <summary>
/// 消息显示到控件
/// </summary>
public void MessageShow(string data)
{
rtbx_ReceiveData.Invoke(new Action(() =>
{
rtbx_ReceiveData.AppendText($"{
DateTime.Now}】{
data}{
System.Environment.NewLine}");
}));
}
private void OnListened(object sender,bool listened)
{
btn_ListenOpen.Invoke(new Action(() =>
{
btn_ListenOpen.Text = listened ? "停止" : "侦听";
ControlStyleUpdata(picBx_ListenStatu, listened ? Color.LimeGreen : Color.Gray);
}));
}
private void OnConnected(object sender, bool connected)
{
btn_Connect.Invoke(new Action(() =>
{
btn_Connect.Text = connected ? "断开" : "连接";
ControlStyleUpdata(picBx_ConnectStatu, connected ? Color.LimeGreen : Color.Gray);
bool isConnected = tibcoRVTHelper.IsConnected;
if (isConnected) //连接成功
{
ControlStyleUpdata(picBx_ConnectStatu, Color.LimeGreen);
if (tibcoRVTHelper.IsListened)
{
ControlStyleUpdata(picBx_ListenStatu, Color.LimeGreen);
ControlUpdata(false);
}
btn_Connect.Text = isConnected ? "断开" : "连接";
}
else //连接失败
{
ControlStyleUpdata(picBx_ConnectStatu, Color.Gray);
ControlStyleUpdata(picBx_ListenStatu, Color.Gray);
btn_Connect.Text = isConnected ? "断开" : "连接";
ControlUpdata(true);
}
}));
}
#endregion
#region 控件圆角
#region 控件圆角方法1
public void ControlStyleUpdata(Control control)
{
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(control.ClientRectangle);
Region region = new Region(gp);
control.Region = region;
gp.Dispose();
region.Dispose();
}
public void ControlStyleUpdata(Control control, Color bcColor)
{
control.BackColor = bcColor;
ControlStyleUpdata(control);
}
#endregion
#region 控件圆角方法2
/// <summary>
/// 按钮控件圆角绘制绑定事件
/// </summary>
private void button_Paint(object sender, PaintEventArgs e)
{
Button button = (Button)sender;
Draw(e.ClipRectangle, e.Graphics, 12, false, Color.FromArgb(0, 122, 204), Color.FromArgb(8, 39, 57));
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString(button.Text, new Font("微软雅黑", 12, FontStyle.Regular), new SolidBrush(Color.White), new PointF(15, 0));
}
/// <summary>
/// 绘制圆角
/// </summary>
private void Draw(Rectangle rectangle, Graphics g, int _radius, bool cusp, Color begin_color, Color end_color)
{
int span = 2;
//抗锯齿
g.SmoothingMode = SmoothingMode.AntiAlias;
//渐变填充
LinearGradientBrush myLinearGradientBrush = new LinearGradientBrush(rectangle, begin_color, end_color, LinearGradientMode.Vertical);
//画尖角
if (cusp)
{
span = 10;
PointF p1 = new PointF(rectangle.Width - 12, rectangle.Y + 10);
PointF p2 = new PointF(rectangle.Width - 12, rectangle.Y + 30);
PointF p3 = new PointF(rectangle.Width, rectangle.Y + 20);
PointF[] ptsArray = {
p1, p2, p3 };
g.FillPolygon(myLinearGradientBrush, ptsArray);
}
//填充
g.FillPath(myLinearGradientBrush, DrawRoundRect(rectangle.X, rectangle.Y, rectangle.Width - span, rectangle.Height - 1, _radius));
}
/// <summary>
/// 设置圆角
/// </summary>
public static GraphicsPath DrawRoundRect(int x, int y, int width, int height, int radius)
{
//四边圆角
GraphicsPath gp = new GraphicsPath();
gp.AddArc(x, y, radius, radius, 180, 90);
gp.AddArc(width - radius, y, radius, radius, 270, 90);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddArc(x, height - radius, radius, radius, 90, 90);
gp.CloseAllFigures();
return gp;
}
#endregion
#endregion
#region 其他辅助方法
public void ControlUpdata(bool controlStatus)
{
foreach (Control control in panel_Top.Controls)
{
if (control is TextBox)
{
control.Enabled = controlStatus;
}
}
}
#endregion
private void btn_ClearReceiveData_Click(object sender, EventArgs e)
{
rtbx_ReceiveData.Text = string.Empty;
}
private void btn_ClearSendData_Click(object sender, EventArgs e)
{
rtbx_SendData.Text = string.Empty;
}
}
}
设计器代码
namespace Demo_TibcoRV
{
partial class Frm_TibcoRV
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel_Top = new System.Windows.Forms.Panel();
this.tbx_MessageField = new System.Windows.Forms