Commit 2200f7f8 by 施晓雨

更新代码

parent 6019ca53
using Kivii; using Kivii;
using Kivii.Text;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
...@@ -18,7 +19,12 @@ namespace Test ...@@ -18,7 +19,12 @@ namespace Test
{ {
InitializeComponent(); InitializeComponent();
} }
#region 连接SSE以及相应的事件响应
/// <summary>
/// 连接SSE
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e) private void btnConnect_Click(object sender, EventArgs e)
{ {
if (_client != null)//断开操作 if (_client != null)//断开操作
...@@ -33,11 +39,11 @@ namespace Test ...@@ -33,11 +39,11 @@ namespace Test
btnConnect.Enabled=false; btnConnect.Enabled=false;
_client = new ServerEventsClient(tbxServiceUrl.Text); _client = new ServerEventsClient(tbxServiceUrl.Text);
_client.OnConnect = onConnected;//连接成功 _client.OnConnect = onConnected;//连接成功
_client.OnCommand = onCommand;//收到命令 _client.OnCommand = sse_OnCommand;//收到命令,指cmd.onJoin,cmd.onLeave,cmd.onUpdate,即有订阅者加入、离开频道,或更新了信息
_client.OnMessage = onMessage;//收到消息 _client.OnMessage = sse_OnMessage;//收到消息
_client.OnException = onException;//发生异常 _client.OnException = sse_OnException;//发生异常
_client.OnHeartbeat = onHeartbeat;//心跳 _client.OnHeartbeat = sse_OnHeartbeat;//心跳
_client.Handlers["test"] = testHandler; _client.Handlers["test"] = sse_OnCustomHandler;//当发送的Selector为cmd.test时,会触发这个事件
_client.Channels = tbxConnectChannels.Text.Split(new char[1] { ','},StringSplitOptions.RemoveEmptyEntries); _client.Channels = tbxConnectChannels.Text.Split(new char[1] { ','},StringSplitOptions.RemoveEmptyEntries);
//如果用户名为空,匿名连接 //如果用户名为空,匿名连接
if (tbxUserName.Text.IsNullOrEmpty()) if (tbxUserName.Text.IsNullOrEmpty())
...@@ -62,13 +68,6 @@ namespace Test ...@@ -62,13 +68,6 @@ namespace Test
} }
private void testHandler(ServerEventsClient source, ServerEventMessage args)
{
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:testHandler {args.Data}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret();
}
//连接成功,得到连接相应的信息 //连接成功,得到连接相应的信息
private void onConnected(ServerEventConnect connection) private void onConnected(ServerEventConnect connection)
{ {
...@@ -91,76 +90,74 @@ namespace Test ...@@ -91,76 +90,74 @@ namespace Test
rtbMessage.ScrollToCaret(); rtbMessage.ScrollToCaret();
} }
//得到心跳包 //得到心跳包
private void onHeartbeat() private void sse_OnHeartbeat()
{ {
if (InvokeRequired) if (InvokeRequired)
{ {
Invoke(new Action(onHeartbeat)); Invoke(new Action(sse_OnHeartbeat));
return; return;
} }
tbxHeartBeatTime.BackColor = tbxHeartBeatTime.BackColor != Color.MistyRose ? Color.MistyRose : Color.YellowGreen; tbxHeartBeatTime.BackColor = tbxHeartBeatTime.BackColor != Color.MistyRose ? Color.MistyRose : Color.YellowGreen;
tbxHeartBeatTime.Text = DateTime.Now.ToString("HH:mm:ss"); tbxHeartBeatTime.Text = DateTime.Now.ToString("HH:mm:ss");
} }
//出现异常 //出现异常
private void onException(Exception ex) private void sse_OnException(Exception ex)
{ {
if (InvokeRequired) if (InvokeRequired)
{ {
Invoke(new Action(()=>onException(ex))); Invoke(new Action(() => sse_OnException(ex)));
return; return;
} }
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onException {ex.Message}{Environment.NewLine}{Environment.NewLine}"); rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onException {ex.Message}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret(); rtbMessage.ScrollToCaret();
} }
//得到消息 //得到消息
private void onMessage(ServerEventMessage message) private void sse_OnMessage(ServerEventMessage message)
{ {
if (InvokeRequired) if (InvokeRequired)
{ {
Invoke(new Action(() => onMessage(message))); Invoke(new Action(() => sse_OnMessage(message)));
return; return;
} }
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onMessage {message.Json}{Environment.NewLine}{Environment.NewLine}"); rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onMessage {message.Json}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret(); rtbMessage.ScrollToCaret();
} }
//得到命令 //得到命令
private void onCommand(ServerEventMessage command) private void sse_OnCommand(ServerEventMessage command)
{ {
if (InvokeRequired) if (InvokeRequired)
{ {
Invoke(new Action(() => onCommand(command))); Invoke(new Action(() => sse_OnCommand(command)));
return; return;
} }
var eventUpdate = command as ServerEventUpdate; var eventUpdate = command as ServerEventUpdate;
if (eventUpdate!=null&&eventUpdate.SubscriptionId==_client.ConnectionInfo.SubscriptionId) if (eventUpdate != null && eventUpdate.SubscriptionId == _client.ConnectionInfo.SubscriptionId)
{ {
tbxChannels.Text = eventUpdate.Channels.Join(","); tbxChannels.Text = eventUpdate.Channels.Join(",");
} }
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:{command.GetType().Name} {command.Json}{Environment.NewLine}{Environment.NewLine}"); rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:{command.GetType().Name} {command.Json}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret(); rtbMessage.ScrollToCaret();
} }
//自定义的
private void sse_OnCustomHandler(ServerEventsClient source, ServerEventMessage args)
private void btnSendMessage_Click(object sender, EventArgs e)
{ {
}
private void btnGet_Click(object sender, EventArgs e) rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:testHandler {args.Data}{Environment.NewLine}{Environment.NewLine}");
{ rtbMessage.ScrollToCaret();
var users = _client.GetChannelSubscribers();
var id = _client.SubscriptionId;
} }
#endregion
#region 订阅频道更新操作
private void btnRemoveChannel_Click(object sender, EventArgs e) private void btnRemoveChannel_Click(object sender, EventArgs e)
{ {
btnRemoveChannel.Enabled = false; btnRemoveChannel.Enabled = false;
var task=_client.UnsubscribeFromChannelsAsync(tbxUpdateChannel.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries)); var task = _client.UnsubscribeFromChannelsAsync(tbxUpdateChannel.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries));
task.Success(()=>{ task.Success(() => {
tbxChannels.Text = _client.Channels.Join(","); tbxChannels.Text = _client.Channels.Join(",");
btnRemoveChannel.Enabled = true; btnRemoveChannel.Enabled = true;
}); });
task.Error((ex)=> { task.Error((ex) => {
btnRemoveChannel.Enabled = true; btnRemoveChannel.Enabled = true;
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onException {ex.Message}{Environment.NewLine}{Environment.NewLine}"); rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onException {ex.Message}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret(); rtbMessage.ScrollToCaret();
...@@ -183,14 +180,37 @@ namespace Test ...@@ -183,14 +180,37 @@ namespace Test
}); });
} }
#endregion
#region 发送消息
private void btnNotify_Click(object sender, EventArgs e)
{
var message = new EventMessageRequest();
message.Channel = cbxToChannel.Checked ? tbxToChannel.Text : null;
message.SubscriptionId = cbxToSubscriptionId.Checked ? tbxToSubscriptionId.Text : null;
message.SessionId = cbxToSessionId.Checked ? tbxToSessionId.Text : null;
message.UserId = cbxToUserId.Checked ? tbxToUserId.Text : null;
message.Message = JsonObject.Parse(rtbToMessage.Text);
_client.SendMessageAsync(message).Success(res=> {
rtbMessage.AppendText($"{Environment.NewLine}发送结果:{res.ToJson()}{Environment.NewLine}");
});
}
#endregion
private void btnGet_Click(object sender, EventArgs e)
{
var users = _client.GetChannelSubscribers();
var id = _client.SubscriptionId;
}
private void btnGetSubscriptions_Click(object sender, EventArgs e) private void btnGetSubscriptions_Click(object sender, EventArgs e)
{ {
btnGetSubscriptions.Enabled = false; btnGetSubscriptions.Enabled = false;
var task = _client.GetChannelSubscribersAsync(); var task = _client.GetChannelSubscribersAsync();
task.Success(() => { task.Success((res) => {
btnGetSubscriptions.Enabled = true; btnGetSubscriptions.Enabled = true;
foreach (var item in task.Result) foreach (var item in res)
{ {
rtbMessage.AppendText($"DisplayName:{item.DisplayName},UserId:{item.UserId},SubscriptionId:{item.SubscriptionId},Channels:{item.Channels.Join()}{Environment.NewLine}"); rtbMessage.AppendText($"DisplayName:{item.DisplayName},UserId:{item.UserId},SubscriptionId:{item.SubscriptionId},Channels:{item.Channels.Join()}{Environment.NewLine}");
rtbMessage.ScrollToCaret(); rtbMessage.ScrollToCaret();
...@@ -208,8 +228,6 @@ namespace Test ...@@ -208,8 +228,6 @@ namespace Test
rtbMessage.Clear(); rtbMessage.Clear();
} }
private void button1_Click(object sender, EventArgs e)
{
}
} }
} }
...@@ -32,9 +32,8 @@ ...@@ -32,9 +32,8 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Kivii.Common.V4.5, Version=5.6.2024.1140, Culture=neutral, processorArchitecture=MSIL"> <Reference Include="Kivii.Common.V4.5, Version=5.6.2024.1150, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion> <HintPath>packages\Kivii.Common.5.6.2024.1150\lib\net45\Kivii.Common.V4.5.dll</HintPath>
<HintPath>..\..\localhost\net45\app\Kivii.Common.V4.5.dll</HintPath>
</Reference> </Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
......
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<packages> <packages>
<package id="Kivii.Common" version="5.6.2024.1140" targetFramework="net45" /> <package id="Kivii.Common" version="5.6.2024.1150" targetFramework="net45" />
</packages> </packages>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment