-
Notifications
You must be signed in to change notification settings - Fork 366
Firefly Reference Example – Write a Simple Echo Server in Firefly
Open cmd and input firefly-admin.py createproject echo_server (input it at terminal in Linux)
Firefly will create an echon_server project in MSI folder in C:\Users
Import the project to eclipse
Configure the corresponding parameter in config.json
Port configuration is inside the master:
Rootport is the admin port of master service
Webport is external http port of master service which realizes the management of all service processes via web interface.
Server configuration is inside the server:
Testserver: server that we need to start
Netport: port that the server monitors
Name: the server name
App: the corresponding file path and name when server starts
App.echo_server: echo_server file in app module
(For multi-server configuration need, such as scene, chat and other more servers in online game developing)
Ddb is database configuration
Host: database service address
User: database user name
Passwd: database password
Port: database connection port
Db: database library name
Charset: database to client connection encoding
Create echo_server.py file in the aforementioned corresponding path in server configuration’s app, here is the code:
Line 3: import netserviceHandle method from Firefly (this method is a defined method in Firefly and could be directly imported and its specific role is to modify logic code of game. As for this example, while the server is monitoring the 1000 port and directive 111 is coming from client, netserviceHandle will import the xxx_111 method in its modifying method according to 111 (echo_111 in this example). So we must avoid directive repetition in game developing).
Line 5: use netserviceHandle as a decorator to modify below functions that need to define.
Line 6: define echo_111 method to handle with requests from clients.
Thus a simplest echo server is completed. When it’s running, it will print messages from client sent and send back to client in original form while continuously monitors the 1000 port.
1) write a simple client, here is the code:
After connection is established, client will call directive 111 method (parameter: hello) in server, analysis and print message that server sends back, and then disconnect in five seconds.
2) Start server
Red line zone shows that the server naming echo_server is working
3) Start client
Red line zone is the result of client connection, it prints hello that client sent, client disconnects in five seconds. Then we look at the client-side:
So client obviously receives message from server, it proves that the server works well.