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".

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.

    Responsive image
  • 3、Create a file named YuanshistOnlineEventsManagerTest.cs and make it inherit the "YuanshistOnlineEventsManager" Class.

  • 4、Create a GameObject in the scene and name it: EventsManager.

    Responsive image
  • Add Class for EventsManager: YuanshistOnlineEventsManagerTest.cs

    Responsive image
  • 5、Select YuanshistOnline, modify the server configuration in the Inspector, and drag EventsManager into the "Event Class" item

    ...
    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

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.

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.
customActionNames
Gets a list of custom events that the client can use, for the server to send data to the client.
公共方法
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.
spawnablePrefabs
Gets or sets the spawnable list.
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
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..
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.
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:

  • int,int32,int64
  • boolean
  • vector3
  • euler(Euler form of rotation)
  • color
  • string(default)

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
Responsive image
  • 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
Responsive image
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.
Client-side system event list
Responsive image
Client-side custom event list
Responsive image
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.

YuanshistOnlineEventsManager Editor

Spawnable Prefabs List
Responsive image

Refer to: Api/YuanshistOnlineEventsManager/Public property

SyncObj Editor

Spawnable Prefabs List