Introduction
YuanshistOnline, a real-time synchronization component based on WebSocket, provides a simple, fast, and flexible networking solution. If not otherwise specified, YuanshistOnline component, hereinafter referred to as "this component".
Current Version: V1.0.2
Support:Windows,Android
Dependencies:
Building a WebSocket service
The WebSocket server supported by this component is Node.js + Express + Socket.io
Node.js : V13.9.0
Express : V4.14.0
socket.io : V2.3.0
Download and Install
Run the Node.js installation file and wait for the installation to complete.
Runtime
Open a Command window and move to the ServerEnd directory of this component.
Wait for the task to complete.
Port
Default,Web Port:1337,Web socket Port:3000
The following content of this article will use the default port to demonstrate.
{
WebPort:1337,
SocketPort:3000
}
Start Server
If all goes well, you will see the following information
xxx/SeverEnd> node app.js
web server started,port:1337
websocket server started,port:3000
Quick Start
This example will use YuanshistOnline to quickly build a small ball synchronization example.
Main: position synchronization, color synchronization
A synchronized ball
-
1、Create a Unity3d project and import YuanshistOnlin.unitypackage
-
2、Drag YuanshistOnline Prefab into the scene.
-
3、Create a file named YuanshistOnlineEventsManagerTest.cs and make it inherit the "YuanshistOnlineEventsManager" Class.
Specific implementation can be viewed: Sample\Scripts\YuanshistOnlineEventsManagerTest.cs -
4、Create a GameObject in the scene and name it: EventsManager.
-
Add Class for EventsManager: YuanshistOnlineEventsManagerTest.cs
-
5、Select YuanshistOnline, modify the server configuration in the Inspector, and drag EventsManager into the "Event Class" item
At this time, if you have started the Server, click the "Verify Events Implement" button to verify whether the related events have been implemented. The specific content of the panel will be explained in detail in the Editor section.
Before verification
After verification -
6、Create a file named BaseInfo.cs to set the color of the balls.
-
7、Create a Sphere named "player" and add two cs files, BaseInfo.cs and SyncObj.cs
SyncObj.cs is included in YuanshistOnlineLib. The specific settings of SyncObj.cs are as follows:
-
8、Create a Sphere again and name it "OtherPlayer". Also add two cs files, BaseInfo.cs and SyncObj.cs
The specific settings of SyncObj.cs are as follows:
-
9、Select EventManager, drag player and OtherPlayer into the corresponding properties
The specific settings are as follows
-
Complete implementation:
Scenes:YuanshistOnlin\Sample\Scenes\TestSample
Prefab:YuanshistOnlin\Sample\Prefab\Sample
Scripts:YuanshistOnlin\Sample\Scripts\Sample
A small ball that fires bullets
-
This example rewrites the basic API in order to bring a more flexible implementation to the user, including the synchronization of the ball position, the synchronization of bullets, etc.
-
Complete implementation:
Scenes:YuanshistOnlin\Sample\Scenes\TestAdvanced
Prefab:YuanshistOnlin\Sample\Prefab\Advanced
Scripts:YuanshistOnlin\Sample\Scripts\Advanced
In this example, WebServer builds two APIs
- All events:http://localhost:1337/lib
- All rooms:http://localhost:1337/rooms
API
This section describes the classes used and some usage methods
YuanshistOnline Class
Basic connection class
Static property
| instance | Get the YuanshistOnline instance. |
Public property
| customActionUrl | Gets or sets the Url of the Event list. It is used to get the Event list from the Web Server. The default is http://localhost:1337/lib. |
| webSocketUrl | Gets or sets the WebSocket server name or IP. The default is localhost. |
| webSocketPort | Gets or sets the port number of the WebSocket server. The default is 3000. |
| AutoConnect | Gets or sets the automatic connection property. The default is false. You need to explicitly call the connect method. |
| Reconnection | Gets or sets whether reconnection is allowed. The default is false. |
| ReconnectionAttempts | Gets or sets the number of reconnections. The default is 5. |
| ReconnectionDelay | Gets or sets the reconnection interval. The default is 1000ms. |
| ReconnectionDelayMax | Gets or sets the maximum reconnection interval. The default is 5000ms. |
| serverActionNames |
Get the list of custom events used by the server, used by the client to send data
to the server.
Examples:socket.Emit("TellServerAddPlayer", new
object[1] { myPlayerObj.ToString() });
|
| customSysActionNames |
Get the list of system events that the client can use, such as connect, reconnect,
etc.
To implement event monitoring, the method must start with "on" and the event
name must be all uppercase.
Examples:public override void onCONNECT()
|
| customActionNames |
Gets a list of custom events that the client can use, for the server to send data
to the client.
To implement event listening, methods must start with "on".
Examples:public override void onTellClient_connected()
|
公共方法
| void VerifiyCustomEvents() | Verify that the client implements those events, including system events (customSysActionNames) and custom events (customActionNames). |
| void connect() | Connects to the WebSocket server and binds implemented events, including system events (customSysActionNames) and custom events (customActionNames). |
| Socket getSocket() | Returns the current WebSocket instance. |
YuanshistOnlineEventsManager Class
Event management class
Static property
| _roomInfo |
Gets or sets the current room information.
{
"clientid": "sjbbQgy1acBmPDaZAAAM",
"room": {
"name": "room1",
"id": "jh567e3u2g8"
}
}
|
Public property
| isUseDefault |
Gets or sets whether to use the default data synchronization method. The default is
true. If a custom data synchronization method is used, it must be set to false.
|
| playerPrefab |
Gets or sets the player's Prefab.
|
| otherPrefab |
Gets or sets the prefab of other players.
The difference between otherPrefab and playerPrefab is the difference in
control behavior.
playerPrefab represents the local control player. Its movement, rotation and
other behaviors are controlled by the user, and the control information is sent
to other players through the server.
otherPrefab represents the remote player, and its behavior is controlled by the
data sent by the remote player.
It is recommended to put the behavior method in a separate Class.
Basic information will appear in both Prefab, such as color information, etc.
|
| spawnablePrefabs |
Gets or sets the spawnable list.
Prefab that is created dynamically and appears only once is generally placed in
this list, such as bullet
Or, Prefab is updated infrequently
This is just a list of shared Prefab, no special operations
|
| kv_spawnablePrefabs |
Gets or sets the KeyValue form of the spawnable list, where Key is the name of the
Prefab.
|
| msgList |
Get list of all synchronized data
If isUserDefault is true, the data in the list will be distributed.
Contains the data of the action attribute, which will be distributed to the
customMsgList attribute for processing separately according to the action
attribute.
The data that does not contain the action attribute will be distributed to
msgDicList according to the name attribute to ensure that the object can quickly
access the data that needs to be expanded.
|
| customMsgList | Get data containing action attributes. |
| msgDicList | Gets data that does not contain the action attribute, in the form of KeyValue, where Key is the name of the object. |
| socket |
Get the current Socket instance.
|
Public method
| virtual void onCONNECT() |
Triggered when the client is connected.
|
| virtual void onRECONNECT_ATTEMPT() |
Trigger an attempt to reconnect.
|
| virtual void onRECONNECT_FAILED() |
重新连接失败时触发
|
| virtual void onRECONNECT_ERROR() |
Triggered when reconnection fails.
|
| virtual void onRECONNECT() |
Triggered on successful reconnection.
|
| virtual void onRECONNECTING() |
Fires when trying to reconnect.
|
| virtual void onCONNECT_ERROR() |
Triggered by connection error.
|
| virtual void onCONNECT_TIMEOUT() |
Triggered when the connection times out.
|
| virtual void onDISCONNECT() |
Triggered when disconnected.
|
| GameObject getOtherPlayer() |
Returns an objectPrefab, if OtherPrefab is null, returns playerPrefab.
|
| virtual void connect() |
Connect to WebSocket server.
|
| virtual void addMsg(object, string="") |
Convert the received data into JObject and add it to msgList.
|
SyncObj Class
Component synchronization class
Public property
| autoSyncOnce |
Gets or sets whether to allow data to be synchronized to the server once. The
default is false..
|
| autoSyncAlways |
Gets or sets whether data is always synchronized to the server. The default is
false..
autoSyncOnce and autoSyncAlways, only one of which can be true at the same time
If both are true, the autoSyncOnce value is valid
|
| autoSyncEvent |
Gets or sets server events. The default is TellClientEnvInfo.
|
| isSyncPosition |
Gets or sets whether the synchronization information contains Position.
|
| isSyncRotation |
Gets or sets whether synchronization information contains Rotation.
|
| isSyncChilds |
Gets or sets whether to include subclass data.
|
| isSyncAttributes |
Gets or sets whether the attribute data is included.
|
| isSyncMethods |
Gets or sets whether method data is included.
|
| autoExpandOnce |
Gets or sets whether to allow data to be synchronized to the server once. The
default is false.
|
| autoExpandAlways |
Gets or sets whether data is always synchronized to the server. The default is
false.
autoExpandOnce and autoExpandAlways, only one of which can be true at the same
time.
If both are true, the autoExpandOnce value is valid.
|
| isExpandPosition |
Gets or sets whether to convert Position.
|
| isExpandRotation |
Gets or sets whether to convert Rotation.
|
| isExpandChilds |
Gets or sets whether to convert subclass data.
|
| isExpandAttributes |
Gets or sets whether to convert attribute data.
|
| isExpandMethods |
Gets or sets whether to convert method data.
|
| syncChildObjs |
Get or set a list of subclasses.
|
| syncAttributes |
Gets or sets a list of attributes.
|
| syncMethods |
Get or set a list of methods.
|
Public method
| virtual void InitStart() |
Initialization method, called in Start
|
| virtual IEnumerator SyncStart() |
The default data synchronization method, sending its own data to the server.
|
| virtual JObject ToJson() |
Data conversion to JObject.
|
| virtual JObject ToJson(bool, bool, bool, bool, bool) |
Data conversion to JObject.
|
| virtual JArray ToChildsJson() |
Subclass list converted to JArray.
|
| virtual JArray ToSyncAttributesJson() |
Convert property list to JArray
|
| virtual JArray ToSyncMethodsJson() |
Method list converted to JArray
|
| virtual void AutoSync() |
The default synchronization method, called in SyncStart
|
| virtual IEnumerator ExpandStart() |
The default data expansion method, filling data into itself
|
| virtual void Expand(JObject) |
The default data expansion method, filling data into itself
|
| virtual void Expand(JObject , bool, bool, bool, bool, bool) |
The default data expansion method, filling data into itself
|
| virtual void SmoothMove(JObject jo) |
Data expansion method, only conver Postion, Rotation and subclasses
|
| virtual object toConvert(Type, JToken) |
Convert JToken to the corresponding Type data
The types that can be converted are as follows:
|
Editor
This section will introduce the use of the Editor of each class
YuanshistOnline Editor
Divided into four parts: basic settings, server-side listening events, client-side system events, and client-side custom events。
Basic settings
- Verify Events Implement(Button) : Used to get the event list from WebServer and verify if the event is implemented
- The other settings have the same meaning as the YuanshistOnline class
Sever side custom event list
| TellServerCreateRoom |
Use when creating or modifying room information
|
| JoinRoom |
Add client to room
|
| TellServerAddPlayer |
Notify other users that a player has joined, and return all users in the
current room.
|
| TellServerSlowUpdate |
Persistent session save data,must have name property.
|
| TellServerPositonRotation |
Notify all users in the room, mainly used for synchronization of Postion
and Rotation.
|
| TellServerEnvInfo |
Notify all users in the room that the current environment has changed.
Generally used for randomly created objects, such as Prefab in the
YuanshistOnlineEventsManager.spawnablePrefabs list.
|
- Separate frequent triggers (such as position changes) from infrequent triggers (such as color changes)
- Separation of non-persistence (such as bullets) from persistence (such as players)
Client-side system event list
Refer to API/YuanshistOnlineEventsManager/公共方法
Client-side custom event list
| TellClientCreateRoom |
Receive room modification events.
|
| TellClient_connected |
Triggered when the connection is successful.
return:
{
"clientid": "sjbbQgy1acBmPDaZAAAM"
}
|
| TellClient_JoinRoomed |
Triggered when the room is successfully added.
return:
{
"clientid": "sjbbQgy1acBmPDaZAAAM",
"room": {
"name": "room1",
"id": "jh567e3u2g8"
}
}
|
| TellClientAddPlayer |
Triggered when a local player is connected to Sever, when a new player joins.
|
| TellClientAllPlayerStatus |
Triggered when the local player is connected to the server, synchronizing the
existing player.
|
| TellClientUpdateAllPlayerStatus |
Triggered when remote player information is modified.
|
| TellClientSlowUpdate |
Triggered when other players persist data.
|
| TellClientPositonRotation |
Triggered when the Player's Postion or Rotation changes. Generally used for
frequent data interactions..
|
| TellClientEnvInfo |
Triggered when other players change their environment, such as firing bullets.
|
| TellClientOtherDisconnect |
Triggered when other players exit or disconnect.
|
- YuanshistOnlineLib/Sample/Scripts/Sample/YuanshistOnlineEventsManagerTest.cs
- YuanshistOnlineLib/Sample/Scripts/Advanced/YuanshistOnlineEventsManagerTestSphere.cs
- Separate frequent triggers (such as position changes) from infrequent triggers (such as color changes)
- Separation of non-persistence (such as bullets) from persistence (such as players)
YuanshistOnlineEventsManager Editor
Basic settings
Spawnable Prefabs List
Refer to: Api/YuanshistOnlineEventsManager/Public property
- This is a shared list of Prefab
- Dynamic creation, which only needs to be synchronized once, has little effect on the entire game environment, such as bullets
- Dynamic creation, relatively low synchronization frequency, such as blood volume