overview
HTML code
Only need reference two Javascript Libraries in HTML:
The first line reference the client function library. This's a dynamic library includes many dynamic components and CDN resources, so don't save to local. For new features, strongly recommend above address which comes with the latest version of X-Server, many new components, enhanced performance, and more. The second line is using third-party socket library from CDN.
Javascript code
Create a client instance
If you connect multiple servers at the same time, you can create multiple client instances. Connect a server,Load authentication module, Provide offline login codes and method, logout codes and method, multi-server login, automatic login, user cache and other functions of authentication. If you don't need these features, you should not execute this line of code.
Using eXtend feature
Feature name can be defined by yourself, starting with lowercase letter and only letters without numbers & symbols. .xxx() should be Get(), Set(), New(), Del(). These are all asynchronous methods, so every asynchronous method needs to be followed by "then" or add "await" in front.
Above codes can also be written as follow:
Sign Up a new user
Login code
Use local cache to automatically login can be written as follow:
Logout code
Monitor user registration
Listen for real-time messages registered by users. This will be a mass message to update other users' interfaces without refreshing.
Get example
Above codes are equivalent to:
Operators of Get Method
Equals a Specified Value
Specifies equality condition. The $eq operator matches documents where the value of a field equals the specified value. The following example queries the user collection to select all documents where the value of the email field equals a:
$max
The $max stage has the following prototype form:
$max takes a positive integer that specifies the maximum number of documents to pass along. Consider the following example:
This operation returns only the first 50 documents. $max has no effect on the content of the documents it passes. Set $max to 0 will get the counting faster.
$sort
Specifies the order in which the returns matching documents. You can apply $sort to sort by and a value of 1 or -1 to specify an ascending or descending sort respectively before retrieving any documents from the database. The following sample document specifies a descending sort by the _time field and will returns the 10 newest documents:
This operation use $max in conjunction with $sort to return the first (in terms of the sort order) documents.
lessThan
$lt selects the documents where the value of the field is less than (i.e. <) the specified value.
Catch an exception
Use try...catch statement marks a block of statements to try, and specifies a response, should an exception be thrown.
Asynchronous catch
For example: Login
For example: Get
Synchronous catch
For example: Login
For example: Get
Last updated