-
Notifications
You must be signed in to change notification settings - Fork 524
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix]修正WebSocket消息组包,Payload可能是链式数据包
- Loading branch information
Showing
11 changed files
with
209 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
using System; | ||
using NewLife.Data; | ||
using NewLife.Http; | ||
using NewLife.Messaging; | ||
using Xunit; | ||
|
||
namespace XUnitTest.Http; | ||
|
||
public class WebSocketMessageTests | ||
{ | ||
[Fact] | ||
public void Text() | ||
{ | ||
var msg = new WebSocketMessage | ||
{ | ||
Type = WebSocketMessageType.Text, | ||
Payload = (ArrayPacket)$"Hello NewLife", | ||
}; | ||
|
||
var pk = msg.ToPacket(); | ||
Assert.Equal("810D48656C6C6F204E65774C696665", pk.ToHex()); | ||
|
||
var msg2 = new WebSocketMessage(); | ||
var rs = msg2.Read(pk); | ||
Assert.True(rs); | ||
|
||
Assert.Equal(msg.Type, msg2.Type); | ||
Assert.Equal(msg.Payload.ToHex(), msg2.Payload.ToHex()); | ||
} | ||
|
||
[Fact] | ||
public void Ping() | ||
{ | ||
var msg = new WebSocketMessage | ||
{ | ||
Type = WebSocketMessageType.Ping, | ||
Payload = (ArrayPacket)$"Ping {DateTime.UtcNow.ToFullString()}", | ||
}; | ||
|
||
var pk = msg.ToPacket(); | ||
Assert.StartsWith("891850696E67", pk.ToHex()); | ||
|
||
var msg2 = new WebSocketMessage(); | ||
var rs = msg2.Read(pk); | ||
Assert.True(rs); | ||
|
||
Assert.Equal(msg.Type, msg2.Type); | ||
Assert.Equal(msg.Payload.ToHex(), msg2.Payload.ToHex()); | ||
} | ||
|
||
[Fact] | ||
public void Close() | ||
{ | ||
var msg = new WebSocketMessage | ||
{ | ||
Type = WebSocketMessageType.Close, | ||
CloseStatus = 1000, | ||
StatusDescription = "Finish", | ||
}; | ||
|
||
var pk = msg.ToPacket(); | ||
Assert.Equal("880803E846696E697368", pk.ToHex()); | ||
|
||
var msg2 = new WebSocketMessage(); | ||
var rs = msg2.Read(pk); | ||
Assert.True(rs); | ||
|
||
Assert.Equal(msg.Type, msg2.Type); | ||
Assert.Equal(msg.CloseStatus, msg2.CloseStatus); | ||
Assert.Equal(msg.StatusDescription, msg2.StatusDescription); | ||
} | ||
|
||
[Fact] | ||
public void DefaultMessageOverWebsocket() | ||
{ | ||
var dm = new DefaultMessage | ||
{ | ||
Flag = 0x01, | ||
Sequence = 0xAB, | ||
Payload = (ArrayPacket)"Hello NewLife" | ||
}; | ||
|
||
var msg = new WebSocketMessage | ||
{ | ||
Type = WebSocketMessageType.Binary, | ||
Payload = dm.ToPacket(), | ||
}; | ||
|
||
var pk = msg.ToPacket(); | ||
Assert.Equal("821101AB0D0048656C6C6F204E65774C696665", pk.ToHex()); | ||
|
||
var msg2 = new WebSocketMessage(); | ||
var rs = msg2.Read(pk); | ||
Assert.True(rs); | ||
|
||
Assert.Equal(msg.Type, msg2.Type); | ||
Assert.Equal(msg.Payload.ToHex(), msg2.Payload.ToHex()); | ||
|
||
var dm2 = new DefaultMessage(); | ||
rs = dm2.Read(msg2.Payload); | ||
Assert.True(rs); | ||
|
||
Assert.Equal(dm.Flag, dm2.Flag); | ||
Assert.Equal(dm.Sequence, dm2.Sequence); | ||
Assert.Equal(dm.Payload.ToHex(), dm2.Payload.ToHex()); | ||
} | ||
} |