Skip to content

Commit

Permalink
客户端适配,服务器添加数据推送模块。
Browse files Browse the repository at this point in the history
  • Loading branch information
dathlin committed May 29, 2018
1 parent 3229b70 commit 31df95b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
5 changes: 3 additions & 2 deletions Client/FormClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Windows.Forms;
using HslCommunication.Enthernet;
using HslCommunication.Core.Net;

namespace Client
{
Expand Down Expand Up @@ -50,12 +51,12 @@ private void NetComplexInitialization()
complexClient.ClientStart();
}

private void ComplexClient_AcceptString(AsyncStateOne stateOne, HslCommunication.NetHandle handle, string data)
private void ComplexClient_AcceptString(AppSession session, HslCommunication.NetHandle handle, string data)
{
// 接收到服务器发送过来的字符串数据时触发
}

private void ComplexClient_AcceptByte(AsyncStateOne stateOne, HslCommunication.NetHandle handle, byte[] buffer)
private void ComplexClient_AcceptByte( AppSession session, HslCommunication.NetHandle handle, byte[] buffer)
{
// 接收到服务器发送过来的字节数据时触发
if (handle == 1)
Expand Down
30 changes: 27 additions & 3 deletions PlcReadTest/FormServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private void FormServer_Load( object sender, EventArgs e )
LogNet.BeforeSaveToFile += LogNet_BeforeSaveToFile; // 设置存储日志前的一些额外操作
NetComplexInitialization( ); // 初始化网络服务
NetSimplifyInitialization( ); // 初始化同步网络服务
NetPushServerInitialization( );
TimerInitialization( ); // 定时器初始化
SiemensTcpNetInitialization( ); // PLC读取初始化
}
Expand Down Expand Up @@ -213,6 +214,28 @@ private void UpdateOnlineClients( )
}
}

#endregion

#region 数据的订阅发布实现

/****************************************************************************************************************
*
* 本模块主要负责进行数据的发布。只要客户端订阅了相关的数据,服务器端进行推送后,客户端就可以收到数据
*
* 因为本订阅器目前只支持字符串的数据订阅,所以在这里需要将byts[]转化成base64编码的数据,相关的知识请自行百度,此处不再说明
*
*****************************************************************************************************************/

private NetPushServer pushServer = null; // 订阅发布核心服务器

private void NetPushServerInitialization( )
{
pushServer = new NetPushServer( );
pushServer.LogNet = LogNet;
pushServer.ServerStart( 23467 );
}


#endregion

#region PLC 数据读取块
Expand Down Expand Up @@ -271,9 +294,10 @@ private void ThreadBackgroundReadPlc( )

if (read.IsSuccess)
{
failed = 0; // 读取失败次数清空
netComplex.SendAllClients( 1, read.Content ); // 群发所有客户端
ShowReadContent( read.Content ); // 在主界面进行显示,此处仅仅是测试,实际项目中不建议在服务端显示数据信息
failed = 0; // 读取失败次数清空
pushServer.PushString( "A", Convert.ToBase64String( read.Content ) ); // 推送数据,关键字为A
netComplex.SendAllClients( 1, read.Content ); // 群发所有客户端
ShowReadContent( read.Content ); // 在主界面进行显示,此处仅仅是测试,实际项目中不建议在服务端显示数据信息


}
Expand Down

0 comments on commit 31df95b

Please sign in to comment.