@@ -42,11 +42,11 @@ public WebSocketServer()
42
42
nd_step . DataBindings . Add ( "Value" , this , "SendInterval" ) ;
43
43
}
44
44
45
- private void OnReceive ( string msg )
45
+ private void OnReceive ( string remote , string msg )
46
46
{
47
47
tbx_received . Invoke ( ( ) =>
48
48
{
49
- tbx_received . AppendText ( DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss.fff " ) ) ;
49
+ tbx_received . AppendText ( string . Format ( "{0} [{1}]{2}" , DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss.fff" ) , remote , Environment . NewLine ) ) ;
50
50
tbx_received . AppendText ( msg ) ;
51
51
tbx_received . Select ( tbx_received . TextLength - msg . Length , msg . Length ) ;
52
52
tbx_received . SelectionColor = Color . FromArgb ( 255 , new Random ( ) . Next ( 0 , 100 ) , new Random ( ) . Next ( 100 , 255 ) , new Random ( ) . Next ( 1 , 128 ) ) ;
@@ -78,9 +78,10 @@ private void btn_start_Click(object sender, EventArgs e)
78
78
else if ( ServerMode == "HTTP" )
79
79
{
80
80
Server . Map ( ServerUrl , ( ) => SendMessage ) ;
81
- }
81
+
82
+ }
82
83
Server . Start ( ) ;
83
- cbx_mode . Enabled = cbx_ip . Enabled = tbx_port . Enabled = tbx_path . Enabled = btn_start . Enabled = false ;
84
+ cbx_mode . Enabled = cbx_ip . Enabled = tbx_port . Enabled = tbx_path . Enabled = btn_start . Enabled = false ;
84
85
btn_stop . Enabled = true ;
85
86
}
86
87
catch ( Exception ex )
@@ -95,33 +96,42 @@ public void WebSocketHandler(IHttpContext content)
95
96
{
96
97
var ws = content . WebSocket ;
97
98
ws . Handler = ProcessMessage ;
98
- OnReceive ( string . Format ( "[{0}]->WebSocket连接建立" , ws . Context . Connection . Remote ) ) ;
99
+ OnReceive ( ws . Context . Connection . Remote . ToString ( ) , "WebSocket连接建立" ) ;
100
+ // 加入客户端列表
99
101
_clients . Add ( ws . Context . Connection . Remote . ToString ( ) , ws ) ;
102
+ if ( cbx_reply . Checked )
103
+ {
104
+ Task . Run ( async ( ) =>
105
+ {
106
+ await Task . Delay ( 500 ) ;
107
+ ws . Send ( "Welcome to WebSocket Server" ) ;
108
+ } ) ;
109
+ }
100
110
}
101
111
102
112
public void ProcessMessage ( WebSocket socket , WebSocketMessage message )
103
113
{
104
- var remote = socket . Context . Connection . Remote ;
114
+ var remote = socket . Context . Connection . Remote . ToString ( ) ;
105
115
var msg = message . Payload . GetSpan ( ) . ToStr ( ) ;
106
116
switch ( message . Type )
107
117
{
108
118
case WebSocketMessageType . Text :
109
- OnReceive ( string . Format ( "[{0}]->{1}" , remote , msg ) ) ;
119
+ OnReceive ( remote , msg ) ;
110
120
// 群发所有客户端
111
121
//socket.SendAll($"[{remote}]说,{msg}");
112
122
if ( cbx_reply . Checked )
113
123
socket . Send ( msg ) ;
114
124
//socket.SendAll(msg, (s) => s.Session.Remote == remote);
115
125
break ;
116
126
case WebSocketMessageType . Close :
117
- OnReceive ( string . Format ( "[{0}]->关闭连接 [ {1}] {2}" , remote , message . CloseStatus , message . StatusDescription ) ) ;
127
+ OnReceive ( remote , string . Format ( "关闭连接 [{0}] {1}" , message . CloseStatus , message . StatusDescription ) ) ;
118
128
break ;
119
129
case WebSocketMessageType . Ping :
120
130
case WebSocketMessageType . Pong :
121
- OnReceive ( string . Format ( "[{0}]->{1}" , remote , msg ) ) ;
131
+ OnMsg ( $ " { remote } { message . Type } " ) ;
122
132
break ;
123
133
default :
124
- OnReceive ( string . Format ( "[{0}]->{1}" , remote , msg ) ) ;
134
+ OnReceive ( remote , msg ) ;
125
135
break ;
126
136
}
127
137
}
0 commit comments