Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
若汝棋茗
RRQMSocket
提交
eef56dd1
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,发现更多精彩内容 >>
提交
eef56dd1
编写于
9月 24, 2022
作者:
若
若汝棋茗
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完善jsonrpc的websocket协议调用
上级
f59af66b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
105 addition
and
69 deletion
+105
-69
examples/JsonRpc简单示例/JsonRpcConsoleApp/Program.cs
examples/JsonRpc简单示例/JsonRpcConsoleApp/Program.cs
+105
-69
未找到文件。
examples/JsonRpc简单示例/JsonRpcConsoleApp/Program.cs
浏览文件 @
eef56dd1
...
...
@@ -9,6 +9,7 @@ using TouchSocket.Core.Config;
using
TouchSocket.Core.Plugins
;
using
TouchSocket.Core.XREF.Newtonsoft.Json.Linq
;
using
TouchSocket.Http
;
using
TouchSocket.Http.WebSockets
;
using
TouchSocket.Rpc
;
using
TouchSocket.Rpc.JsonRpc
;
using
TouchSocket.Rpc.TouchRpc
;
...
...
@@ -16,83 +17,31 @@ using TouchSocket.Sockets;
namespace
JsonRpcConsoleApp
{
public
class
JsonRpcServer
:
RpcServer
{
/// <summary>
/// 使用调用上下文。
/// 可以从上下文获取调用的SocketClient。从而获得IP和Port等相关信息。
/// </summary>
/// <param name="callContext"></param>
/// <param name="str"></param>
/// <returns></returns>
[
JsonRpc
(
MethodFlags
=
MethodFlags
.
IncludeCallContext
)]
public
string
TestGetContext
(
ICallContext
callContext
,
string
str
)
{
if
(
callContext
.
Caller
is
HttpSocketClient
socketClient
)
{
Console
.
WriteLine
(
"HTTP请求"
);
var
client
=
callContext
.
Caller
as
HttpSocketClient
;
var
ip
=
client
.
IP
;
var
port
=
client
.
Port
;
Console
.
WriteLine
(
$"HTTP请求
{
ip
}
:
{
port
}
"
);
}
else
if
(
callContext
.
Caller
is
SocketClient
)
{
Console
.
WriteLine
(
"Tcp请求"
);
var
client
=
callContext
.
Caller
as
SocketClient
;
var
ip
=
client
.
IP
;
var
port
=
client
.
Port
;
Console
.
WriteLine
(
$"Tcp请求
{
ip
}
:
{
port
}
"
);
}
return
"RRQM"
+
str
;
}
[
JsonRpc
]
public
JObject
TestJObject
(
JObject
obj
)
{
return
obj
;
}
[
JsonRpc
]
public
string
TestJsonRpc
(
string
str
)
{
return
"RRQM"
+
str
;
}
/// <summary>
/// 当标记为true时直接使用方法名称
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[
JsonRpc
(
true
)]
public
string
TestJsonRpc1
(
string
str
)
{
return
"RRQM"
+
str
;
}
}
internal
class
Program
{
private
static
IRpcParser
CreateHTTPJsonRpcParser
(
int
port
)
private
static
void
CreateHTTPJsonRpcParser
(
int
port
)
{
HttpService
service
=
new
HttpService
();
service
.
Setup
(
new
TouchSocketConfig
()
.
UsePlugin
()
.
SetListenIPHosts
(
new
IPHost
[]
{
new
IPHost
(
port
)
})
.
ConfigureRpcStore
(
a
=>
.
ConfigureRpcStore
(
a
=>
//Rpc的配置必须在插件之前。
{
a
.
RegisterServer
<
JsonRpcServer
>();
})
.
ConfigurePlugins
(
a
=>
{
a
.
Add
<
WebSocketServerPlugin
>()
.
SetWSUrl
(
"/ws"
);
//启用websocket,使用/ws路由连接。
a
.
Add
<
JsonRpcParserPlugin
>()
.
SetJsonRpcUrl
(
"/jsonRpc"
);
}))
.
Start
();
return
service
.
AddPlugin
<
JsonRpcParserPlugin
>()
.
SetJsonRpcUrl
(
"/jsonRpc"
);
}
private
static
IRpcParser
CreateTcpJsonRpcParser
(
int
port
)
private
static
void
CreateTcpJsonRpcParser
(
int
port
)
{
TcpService
service
=
new
TcpService
();
service
.
Connecting
+=
(
client
,
e
)
=>
...
...
@@ -114,13 +63,15 @@ namespace JsonRpcConsoleApp
.
UsePlugin
()
.
SetDataHandlingAdapter
(()
=>
new
TerminatorPackageAdapter
(
"\r\n"
))
//使用这个适配器,防止粘包。但是也要求json中没有换行符。
.
SetListenIPHosts
(
new
IPHost
[]
{
new
IPHost
(
port
)
})
.
ConfigureRpcStore
(
a
=>
.
ConfigureRpcStore
(
a
=>
{
a
.
RegisterServer
<
JsonRpcServer
>();
}))
})
.
ConfigurePlugins
(
a
=>
{
a
.
Add
<
JsonRpcParserPlugin
>();
}))
.
Start
();
return
service
.
AddPlugin
<
JsonRpcParserPlugin
>();
}
private
static
void
JsonRpcClientInvokeByHttp
()
...
...
@@ -147,7 +98,7 @@ namespace JsonRpcConsoleApp
JsonRpcClient
jsonRpcClient
=
new
JsonRpcClient
();
jsonRpcClient
.
Setup
(
new
TouchSocketConfig
()
.
SetRemoteIPHost
(
"127.0.0.1:7705"
)
.
SetDataHandlingAdapter
(()=>
new
TerminatorPackageAdapter
(
"\r\n"
))
.
SetDataHandlingAdapter
(()
=>
new
TerminatorPackageAdapter
(
"\r\n"
))
.
SetJRPT
(
JRPT
.
Tcp
));
jsonRpcClient
.
Connect
();
...
...
@@ -169,6 +120,33 @@ namespace JsonRpcConsoleApp
Console
.
WriteLine
(
$"Tcp返回结果:
{
newObj
}
"
);
}
private
static
void
JsonRpcClientInvokeByWebsocket
()
{
JsonRpcClient
jsonRpcClient
=
new
JsonRpcClient
();
jsonRpcClient
.
Setup
(
new
TouchSocketConfig
()
.
SetRemoteIPHost
(
"ws://127.0.0.1:7706/ws"
)
//此url就是能连接到websocket的路径。
.
SetDataHandlingAdapter
(()
=>
new
TerminatorPackageAdapter
(
"\r\n"
))
.
SetJRPT
(
JRPT
.
Websocket
));
jsonRpcClient
.
Connect
();
Console
.
WriteLine
(
"连接成功"
);
string
result
=
jsonRpcClient
.
TestJsonRpc
(
"RRQM"
);
Console
.
WriteLine
(
$"Websocket返回结果:
{
result
}
"
);
result
=
jsonRpcClient
.
TestJsonRpc1
(
"RRQM"
);
Console
.
WriteLine
(
$"Websocket返回结果:
{
result
}
"
);
result
=
jsonRpcClient
.
TestGetContext
(
"RRQM"
);
Console
.
WriteLine
(
$"Websocket返回结果:
{
result
}
"
);
JObject
obj
=
new
JObject
();
obj
.
Add
(
"A"
,
"A"
);
obj
.
Add
(
"B"
,
10
);
obj
.
Add
(
"C"
,
100.1
);
JObject
newObj
=
jsonRpcClient
.
TestJObject
(
obj
);
Console
.
WriteLine
(
$"Websocket返回结果:
{
newObj
}
"
);
}
//JSONRPC 通讯基础示例 TPC和HTTP 未来考虑扩展升级WebSocket
//1.完成了JSONRPC 的基本调用方法
//2.JSONRPC 服务端和客户端的创建
...
...
@@ -190,11 +168,68 @@ namespace JsonRpcConsoleApp
JsonRpcClientInvokeByTcp
();
JsonRpcClientInvokeByHttp
();
JsonRpcClientInvokeByWebsocket
();
Console
.
WriteLine
(
"请按任意键退出"
);
Console
.
ReadKey
();
}
}
public
class
JsonRpcServer
:
RpcServer
{
/// <summary>
/// 使用调用上下文。
/// 可以从上下文获取调用的SocketClient。从而获得IP和Port等相关信息。
/// </summary>
/// <param name="callContext"></param>
/// <param name="str"></param>
/// <returns></returns>
[
JsonRpc
(
MethodFlags
=
MethodFlags
.
IncludeCallContext
)]
public
string
TestGetContext
(
ICallContext
callContext
,
string
str
)
{
if
(
callContext
.
Caller
is
HttpSocketClient
socketClient
)
{
Console
.
WriteLine
(
"HTTP请求"
);
var
client
=
callContext
.
Caller
as
HttpSocketClient
;
var
ip
=
client
.
IP
;
var
port
=
client
.
Port
;
Console
.
WriteLine
(
$"HTTP请求
{
ip
}
:
{
port
}
"
);
}
else
if
(
callContext
.
Caller
is
SocketClient
)
{
Console
.
WriteLine
(
"Tcp请求"
);
var
client
=
callContext
.
Caller
as
SocketClient
;
var
ip
=
client
.
IP
;
var
port
=
client
.
Port
;
Console
.
WriteLine
(
$"Tcp请求
{
ip
}
:
{
port
}
"
);
}
return
"RRQM"
+
str
;
}
[
JsonRpc
]
public
JObject
TestJObject
(
JObject
obj
)
{
return
obj
;
}
[
JsonRpc
]
public
string
TestJsonRpc
(
string
str
)
{
return
"RRQM"
+
str
;
}
/// <summary>
/// 当标记为true时直接使用方法名称
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
[
JsonRpc
(
true
)]
public
string
TestJsonRpc1
(
string
str
)
{
return
"RRQM"
+
str
;
}
}
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录