Firefly Reference Example – Write a Simple Chat Room - 9miao/Firefly GitHub Wiki
Open cmd and input firefly-admin.py createproject chat_rooms (input it at terminal in Linux)
Firefly will create a chat_rooms project in MSI floder 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 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 server.py file in the aforementioned corresponding path in server configuration’s app, here is the code:
Line3: 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 1000 port and directive 111 is coming from client, netserviceHandle will import the xxx_111 method in its modifying method according to 111 (speak_111 in this example). So we must avoid directive repetition in game developing).
Line 7 and 16: define operations after connection made and lost (user online/offline) respectively.
Line 23 and 24: bind the above customized method to framework
Line 26: use netserviceHandle as a decorator to modify below functions that need to define
Line 27: define speak_111 method to handle with requests from clients.
Thus a simplest chat server is completed. When it’s running, it will send messages from client to other online users to realizing chat function while continuously monitors the 1000 port.
1) write a simple client, here is the code:
Client will enable two threads after connection is made; one executes method of sending message, the other executes method of receiving message.
2) Start server
Red line zone shows that the server naming chat_server is working
3) Start client
Now we can start chatting, client 1 say hello to client 2 and client 3 interrupting:
It proves that the server works well.











