Commit 2200f7f8 by 施晓雨

更新代码

parent 6019ca53
using Kivii;
using Kivii.Text;
using System;
using System.Collections.Generic;
using System.ComponentModel;
......@@ -18,7 +19,12 @@ namespace Test
{
InitializeComponent();
}
#region 连接SSE以及相应的事件响应
/// <summary>
/// 连接SSE
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnConnect_Click(object sender, EventArgs e)
{
if (_client != null)//断开操作
......@@ -33,11 +39,11 @@ namespace Test
btnConnect.Enabled=false;
_client = new ServerEventsClient(tbxServiceUrl.Text);
_client.OnConnect = onConnected;//连接成功
_client.OnCommand = onCommand;//收到命令
_client.OnMessage = onMessage;//收到消息
_client.OnException = onException;//发生异常
_client.OnHeartbeat = onHeartbeat;//心跳
_client.Handlers["test"] = testHandler;
_client.OnCommand = sse_OnCommand;//收到命令,指cmd.onJoin,cmd.onLeave,cmd.onUpdate,即有订阅者加入、离开频道,或更新了信息
_client.OnMessage = sse_OnMessage;//收到消息
_client.OnException = sse_OnException;//发生异常
_client.OnHeartbeat = sse_OnHeartbeat;//心跳
_client.Handlers["test"] = sse_OnCustomHandler;//当发送的Selector为cmd.test时,会触发这个事件
_client.Channels = tbxConnectChannels.Text.Split(new char[1] { ','},StringSplitOptions.RemoveEmptyEntries);
//如果用户名为空,匿名连接
if (tbxUserName.Text.IsNullOrEmpty())
......@@ -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)
{
......@@ -91,76 +90,74 @@ namespace Test
rtbMessage.ScrollToCaret();
}
//得到心跳包
private void onHeartbeat()
private void sse_OnHeartbeat()
{
if (InvokeRequired)
{
Invoke(new Action(onHeartbeat));
Invoke(new Action(sse_OnHeartbeat));
return;
}
tbxHeartBeatTime.BackColor = tbxHeartBeatTime.BackColor != Color.MistyRose ? Color.MistyRose : Color.YellowGreen;
tbxHeartBeatTime.Text = DateTime.Now.ToString("HH:mm:ss");
}
//出现异常
private void onException(Exception ex)
private void sse_OnException(Exception ex)
{
if (InvokeRequired)
{
Invoke(new Action(()=>onException(ex)));
Invoke(new Action(() => sse_OnException(ex)));
return;
}
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onException {ex.Message}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret();
}
//得到消息
private void onMessage(ServerEventMessage message)
private void sse_OnMessage(ServerEventMessage message)
{
if (InvokeRequired)
{
Invoke(new Action(() => onMessage(message)));
Invoke(new Action(() => sse_OnMessage(message)));
return;
}
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onMessage {message.Json}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret();
}
//得到命令
private void onCommand(ServerEventMessage command)
private void sse_OnCommand(ServerEventMessage command)
{
if (InvokeRequired)
{
Invoke(new Action(() => onCommand(command)));
Invoke(new Action(() => sse_OnCommand(command)));
return;
}
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(",");
}
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:{command.GetType().Name} {command.Json}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret();
}
private void btnSendMessage_Click(object sender, EventArgs e)
//自定义的
private void sse_OnCustomHandler(ServerEventsClient source, ServerEventMessage args)
{
}
private void btnGet_Click(object sender, EventArgs e)
{
var users = _client.GetChannelSubscribers();
var id = _client.SubscriptionId;
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:testHandler {args.Data}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret();
}
#endregion
#region 订阅频道更新操作
private void btnRemoveChannel_Click(object sender, EventArgs e)
{
btnRemoveChannel.Enabled = false;
var task=_client.UnsubscribeFromChannelsAsync(tbxUpdateChannel.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries));
task.Success(()=>{
var task = _client.UnsubscribeFromChannelsAsync(tbxUpdateChannel.Text.Split(new char[1] { ',' }, StringSplitOptions.RemoveEmptyEntries));
task.Success(() => {
tbxChannels.Text = _client.Channels.Join(",");
btnRemoveChannel.Enabled = true;
});
task.Error((ex)=> {
task.Error((ex) => {
btnRemoveChannel.Enabled = true;
rtbMessage.AppendText($"{DateTime.Now.ToString("HH:mm:ss")}:onException {ex.Message}{Environment.NewLine}{Environment.NewLine}");
rtbMessage.ScrollToCaret();
......@@ -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)
{
btnGetSubscriptions.Enabled = false;
var task = _client.GetChannelSubscribersAsync();
task.Success(() => {
task.Success((res) => {
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.ScrollToCaret();
......@@ -208,8 +228,6 @@ namespace Test
rtbMessage.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
......@@ -32,9 +32,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Kivii.Common.V4.5, Version=5.6.2024.1140, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\localhost\net45\app\Kivii.Common.V4.5.dll</HintPath>
<Reference Include="Kivii.Common.V4.5, Version=5.6.2024.1150, Culture=neutral, processorArchitecture=MSIL">
<HintPath>packages\Kivii.Common.5.6.2024.1150\lib\net45\Kivii.Common.V4.5.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
......
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Kivii.Common" version="5.6.2024.1140" targetFramework="net45" />
<package id="Kivii.Common" version="5.6.2024.1150" targetFramework="net45" />
</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