Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
若汝棋茗
RRQMSocket
提交
a65dcd64
RRQMSocket
项目概览
若汝棋茗
/
RRQMSocket
11 个月 前同步成功
通知
20
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
RRQMSocket
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a65dcd64
编写于
7月 27, 2022
作者:
若
若汝棋茗
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修复WebSocket连接问题
上级
be5a7938
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
103 addition
and
158 deletion
+103
-158
src/TouchSocket/Http/WebSockets/Common/WSTools.cs
src/TouchSocket/Http/WebSockets/Common/WSTools.cs
+102
-157
src/TouchSocket/TouchSocket.csproj
src/TouchSocket/TouchSocket.csproj
+1
-1
未找到文件。
src/TouchSocket/Http/WebSockets/Common/WSTools.cs
浏览文件 @
a65dcd64
...
...
@@ -11,65 +11,10 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/* 项目“TouchSocketPro (net5)”的未合并的更改
在此之前:
using TouchSocket.Core;
在此之后:
using System;
using System.Text;
using TouchSocket.Core;
*/
/* 项目“TouchSocketPro (netcoreapp3.1)”的未合并的更改
在此之前:
using TouchSocket.Core;
在此之后:
using System;
using System.Text;
using TouchSocket.Core;
*/
/* 项目“TouchSocketPro (netstandard2.0)”的未合并的更改
在此之前:
using TouchSocket.Core;
在此之后:
using System;
using System.Text;
using TouchSocket.Core;
*/
using
System
;
using
System.Text
;
using
TouchSocket.Core
;
using
TouchSocket.Core.ByteManager
;
/* 项目“TouchSocketPro (net5)”的未合并的更改
在此之前:
using TouchSocket.Http;
using System;
using System.Text;
在此之后:
using TouchSocket.Http;
*/
/* 项目“TouchSocketPro (netcoreapp3.1)”的未合并的更改
在此之前:
using TouchSocket.Http;
using System;
using System.Text;
在此之后:
using TouchSocket.Http;
*/
/* 项目“TouchSocketPro (netstandard2.0)”的未合并的更改
在此之前:
using TouchSocket.Http;
using System;
using System.Text;
在此之后:
using TouchSocket.Http;
*/
using
TouchSocket.Core.Extensions
;
namespace
TouchSocket.Http.WebSockets
...
...
@@ -84,108 +29,6 @@ namespace TouchSocket.Http.WebSockets
/// </summary>
public
const
string
acceptMask
=
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
;
/// <summary>
/// 获取Base64随即字符串。
/// </summary>
/// <returns></returns>
public
static
string
CreateBase64Key
()
{
var
src
=
new
byte
[
16
];
new
Random
().
NextBytes
(
src
);
return
Convert
.
ToBase64String
(
src
);
}
/// <summary>
/// 获取WS的请求头
/// </summary>
/// <param name="host"></param>
/// <param name="url"></param>
/// <param name="version"></param>
/// <param name="base64Key"></param>
/// <returns></returns>
public
static
HttpRequest
GetWSRequest
(
string
host
,
string
url
,
string
version
,
out
string
base64Key
)
{
HttpRequest
request
=
new
HttpRequest
{
Method
=
"GET"
};
request
.
SetUrl
(
url
);
request
.
Protocols
=
"HTTP"
;
request
.
ProtocolVersion
=
"1.1"
;
request
.
SetHeader
(
HttpHeaders
.
Host
,
host
);
request
.
SetHeader
(
HttpHeaders
.
Connection
,
"Upgrade"
);
request
.
SetHeader
(
HttpHeaders
.
Pragma
,
"no-cache"
);
request
.
SetHeader
(
HttpHeaders
.
UserAgent
,
"TouchSocket.Http.WebSockets"
);
request
.
SetHeader
(
HttpHeaders
.
Upgrade
,
"websocket"
);
request
.
SetHeader
(
HttpHeaders
.
Origin
,
"RRQM"
);
request
.
SetHeader
(
"sec-websocket-version"
,
$"
{
version
}
"
);
request
.
SetHeader
(
HttpHeaders
.
AcceptEncoding
,
"deflate, br"
);
base64Key
=
CreateBase64Key
();
request
.
SetHeader
(
"sec-websocket-key"
,
$"
{
base64Key
}
"
);
return
request
;
}
/// <summary>
/// 计算Base64值
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public
static
string
CalculateBase64Key
(
string
str
,
Encoding
encoding
)
{
return
(
str
+
acceptMask
).
ToSha1
(
encoding
).
ToBase64
();
}
/// <summary>
/// 获取响应
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
/// <returns></returns>
public
static
bool
TryGetResponse
(
HttpRequest
request
,
HttpResponse
response
)
{
string
upgrade
=
request
.
GetHeader
(
HttpHeaders
.
Upgrade
);
if
(
string
.
IsNullOrEmpty
(
upgrade
))
{
return
false
;
}
string
connection
=
request
.
GetHeader
(
HttpHeaders
.
Connection
);
if
(
string
.
IsNullOrEmpty
(
connection
))
{
return
false
;
}
string
secWebSocketKey
=
request
.
GetHeader
(
"sec-websocket-key"
);
if
(
string
.
IsNullOrEmpty
(
secWebSocketKey
))
{
return
false
;
}
response
.
StatusCode
=
"101"
;
response
.
StatusMessage
=
"switching protocols"
;
response
.
SetHeader
(
HttpHeaders
.
Connection
,
"upgrade"
);
response
.
SetHeader
(
HttpHeaders
.
Upgrade
,
"websocket"
);
response
.
SetHeader
(
"sec-websocket-accept"
,
CalculateBase64Key
(
secWebSocketKey
,
request
.
Encoding
));
return
true
;
}
/// <summary>
/// 掩码运算
/// </summary>
/// <param name="storeBuf"></param>
/// <param name="sOffset"></param>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="length"></param>
/// <param name="masks"></param>
public
static
void
DoMask
(
byte
[]
storeBuf
,
int
sOffset
,
byte
[]
buffer
,
int
offset
,
int
length
,
byte
[]
masks
)
{
for
(
var
i
=
0
;
i
<
length
;
i
++)
{
storeBuf
[
sOffset
+
i
]
=
(
byte
)(
buffer
[
offset
+
i
]
^
masks
[
i
%
4
]);
}
}
/// <summary>
/// 构建数据
/// </summary>
...
...
@@ -264,5 +107,107 @@ namespace TouchSocket.Http.WebSockets
}
return
true
;
}
/// <summary>
/// 计算Base64值
/// </summary>
/// <param name="str"></param>
/// <param name="encoding"></param>
/// <returns></returns>
public
static
string
CalculateBase64Key
(
string
str
,
Encoding
encoding
)
{
return
(
str
+
acceptMask
).
ToSha1
(
encoding
).
ToBase64
();
}
/// <summary>
/// 获取Base64随即字符串。
/// </summary>
/// <returns></returns>
public
static
string
CreateBase64Key
()
{
var
src
=
new
byte
[
16
];
new
Random
().
NextBytes
(
src
);
return
Convert
.
ToBase64String
(
src
);
}
/// <summary>
/// 掩码运算
/// </summary>
/// <param name="storeBuf"></param>
/// <param name="sOffset"></param>
/// <param name="buffer"></param>
/// <param name="offset"></param>
/// <param name="length"></param>
/// <param name="masks"></param>
public
static
void
DoMask
(
byte
[]
storeBuf
,
int
sOffset
,
byte
[]
buffer
,
int
offset
,
int
length
,
byte
[]
masks
)
{
for
(
var
i
=
0
;
i
<
length
;
i
++)
{
storeBuf
[
sOffset
+
i
]
=
(
byte
)(
buffer
[
offset
+
i
]
^
masks
[
i
%
4
]);
}
}
/// <summary>
/// 获取WS的请求头
/// </summary>
/// <param name="host"></param>
/// <param name="url"></param>
/// <param name="version"></param>
/// <param name="base64Key"></param>
/// <returns></returns>
public
static
HttpRequest
GetWSRequest
(
string
host
,
string
url
,
string
version
,
out
string
base64Key
)
{
HttpRequest
request
=
new
HttpRequest
{
Method
=
"GET"
,
Protocols
=
"HTTP"
,
ProtocolVersion
=
"1.1"
};
request
.
SetUrl
(
url
);
request
.
SetHeader
(
HttpHeaders
.
Host
,
host
);
request
.
SetHeader
(
HttpHeaders
.
Pragma
,
"no-cache"
);
request
.
SetHeader
(
HttpHeaders
.
UserAgent
,
"TouchSocket.Http.WebSockets"
);
request
.
SetHeader
(
HttpHeaders
.
Origin
,
"RRQM"
);
request
.
SetHeader
(
HttpHeaders
.
AcceptEncoding
,
"deflate, br"
);
request
.
SetHeaderByKey
(
"Connection"
,
"upgrade"
);
request
.
SetHeaderByKey
(
"Upgrade"
,
"websocket"
);
request
.
SetHeaderByKey
(
"Sec-WebSocket-Version"
,
$"
{
version
}
"
);
base64Key
=
CreateBase64Key
();
request
.
SetHeaderByKey
(
"Sec-WebSocket-Key"
,
base64Key
);
return
request
;
}
/// <summary>
/// 获取响应
/// </summary>
/// <param name="request"></param>
/// <param name="response"></param>
/// <returns></returns>
public
static
bool
TryGetResponse
(
HttpRequest
request
,
HttpResponse
response
)
{
string
upgrade
=
request
.
GetHeader
(
HttpHeaders
.
Upgrade
);
if
(
string
.
IsNullOrEmpty
(
upgrade
))
{
return
false
;
}
string
connection
=
request
.
GetHeader
(
HttpHeaders
.
Connection
);
if
(
string
.
IsNullOrEmpty
(
connection
))
{
return
false
;
}
string
secWebSocketKey
=
request
.
GetHeader
(
"sec-websocket-key"
);
if
(
string
.
IsNullOrEmpty
(
secWebSocketKey
))
{
return
false
;
}
response
.
StatusCode
=
"101"
;
response
.
StatusMessage
=
"switching protocols"
;
response
.
SetHeader
(
HttpHeaders
.
Connection
,
"upgrade"
);
response
.
SetHeader
(
HttpHeaders
.
Upgrade
,
"websocket"
);
response
.
SetHeader
(
"sec-websocket-accept"
,
CalculateBase64Key
(
secWebSocketKey
,
request
.
Encoding
));
return
true
;
}
}
}
\ No newline at end of file
src/TouchSocket/TouchSocket.csproj
浏览文件 @
a65dcd64
...
...
@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>RRQM.pfx</AssemblyOriginatorKeyFile>
<Version>0.2.
2
</Version>
<Version>0.2.
3
</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录