-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.md
199 lines (138 loc) · 4.56 KB
/
README.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# Communication
[![NuGet](https://img.shields.io/nuget/v/JSSoft.Communication.svg?style=flat)][NuGet]
[![NuGet (prerelease)](https://img.shields.io/nuget/vpre/JSSoft.Communication.svg?style=flat)][NuGet]
[NuGet]: https://www.nuget.org/packages/JSSoft.Communication/
## Summary
The Communication library is network module using <https://github.com/grpc/grpc>.
## Development Environment
```plain
.NET 8.0
c# 12.0
```
## Clone
```bash
git clone https://github.com/s2quake/communication.git
cd communication
```
## Build
```bash
dotnet build
```
## Run server
```bash
dotnet run --project src/JSSoft.Communication.Server
```
## Run client
```bash
dotnet run --project src/JSSoft.Communication.Client
```
## Quick start
### 1. Create solution
Run terminal and execute the command line below to create a solution and project
```plain
mkdir .example
dotnet new sln -o .example
dotnet new console -o .example/Server-Test
dotnet new console -o .example/Client-Test
dotnet sln .example add .example/Server-Test
dotnet sln .example add .example/Client-Test
dotnet add .example/Server-Test reference src/JSSoft.Communication
dotnet add .example/Client-Test reference src/JSSoft.Communication
```
### 2. Define service
Create `IMyService.cs` file in the path .example/Server-Test.
Then copy and paste the code below into the file `IMyService.cs`.
```csharp
namespace Services;
public interface IMyService
{
Task<Guid> LoginAsync(string userID, CancellationToken cancellationToken);
Task<(string product, string version)> GetVersionAsync(CancellationToken cancellationToken);
}
```
### 3. Implement service
Create `MyService.cs` file in the path .example/Server-Test.
Then copy and paste the code below into the file `MyService.cs`.
```csharp
using JSSoft.Communication;
namespace Services;
sealed class MyService : ServerService<IMyService>, IMyService
{
public Task<Guid> LoginAsync(string userID, CancellationToken cancellationToken)
{
return Task.Run(() =>
{
Console.WriteLine($"logged in: '{userID}'");
return Guid.NewGuid();
}, cancellationToken);
}
public Task<(string product, string version)> GetVersionAsync(CancellationToken cancellationToken)
{
return Task.Run(() =>
{
return ("MyServer", "1.0.0.0");
}, cancellationToken);
}
}
```
## 4. Implement code to run the server
Create `Program.cs` file in the path .example/Server-Test.
Then copy and paste the code below into the file `Program.cs`.
```csharp
using JSSoft.Communication;
using JSSoft.Communication.Extensions;
using Services;
var service = new MyService();
var serviceContext = new ServerContext([service]);
var token = await serviceContext.OpenAsync(cancellationToken: default);
Console.WriteLine("Server has been started.");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
var exitCode = await serviceContext.ReleaseAsync(token);
Environment.Exit(exitCode);
```
## 5. Implement code to run the client
First, you need to include the file `IMyService.cs` that you created when you created the Server-Test project.
Open `.example/Client-Test/Client-Test.csproj` and insert `<Compile Include="..\Server-Test\IMyService.cs" />` into the file as shown below.
```xml
...
<ItemGroup>
<ProjectReference Include="..\..\src\JSSoft.Communication\JSSoft.Communication.csproj" />
<!-- Add this line -->
<Compile Include="..\Server-Test\IMyService.cs" />
</ItemGroup>
...
```
And create `Program.cs` file in the path .example/Server-Test.
Then copy and paste the code below into the file `Program.cs`.
```csharp
using JSSoft.Communication;
using JSSoft.Communication.Extensions;
using Services;
var service = new ClientService<IMyService>();
var serviceContext = new ClientContext([service]);
var token = await serviceContext.OpenAsync(cancellationToken: default);
var server = service.Server;
var id = await server.LoginAsync("admin", cancellationToken: default);
var (product, version) = await server.GetVersionAsync(cancellationToken: default);
Console.WriteLine($"logged in: {id}");
Console.WriteLine($"product: {product}");
Console.WriteLine($"version: {version}");
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
var exitCode = await serviceContext.ReleaseAsync(token);
Environment.Exit(exitCode);
```
## 6. Build and Run example
Build example solution
```bash
dotnet build .example
```
Run Server-Test example project in terminal
```bash
dotnet run --project .example/Server-Test
```
Run Client-Test example project in a new terminal
```bash
dotnet run --project .example/Client-Test
```