Original address : http://nodejs.netease.com/topic/515251b97f53b3d3330025c7
Using a handy IDE to develop applications can make our work more efficient. Debugging allows us to accurately locate bugs and find problems.
This article describes how to use WebStorm, a monster-level JavaScript IDE, to debug Chat of Pomelo .
If you prefer other IDEs such as Eclipse , I hope this article can also be of some help.
foreword
* First of all, we need to know that the NodeJS application can add --debug=5858
parameters to the running command, which can make the NodeJS program listen to the local 5858
port and enable the debugging mode.
-
Secondly, the operating principle of Pomelo is:
pomelo start
when the server is startedmaster
, it first reads the configuration file, and then startsgame-server/config/servers.json
each server process configured in the file.id
Among them, the ,host
, and so on you seeport
are the startup parameters of the sub-server, and these parameters will be placedapp.js
behind the startup command line. -
Again, the designers of Pomelo have initially considered the easy way to add parameters directly before and
node
afterapp.js
: in the server configuration file,"args": " 你想要的参数 "
after the configuration entry is inserted, it can run with the parameters you specifynode
.
Modify the configuration to start the server with debug mode
1. Open the file
open game-server/config/servers.json
. For ease of development, we reduced the number of servers to 1 running instance per function. Then you will see:
2. Modify and save
We gate
became interested in how server load distribution works and wanted to debug it. At this time, we only need to insert the following code at {"id": "gate-server-1", ...
the end of the line, }
before the curly braces , "args": " --debug=32312 "
. Make gete
the server's config group look like this:
When the server is started again, master
the server will read the new configuration and run gate-server-1
the server with this configuration.
The process of the server will listen to 32312
the port so that the debugger can connect. Setting the port higher is to reduce the possibility of port conflicts. ADDRINUSE
During the development process, if you find an error (the port is already occupied) after startup , please use pomelo kill --force
the kill Pomelo program and adjust the port value here.
Configure the IDE's debugger
1. Open Edit Configurations...
the dialog box
Run
You can enter it from the small triangle in front of the run and debug buttons, or select - from the toolbar Edit Configurations...
.
2. Add a newNode.js Remote Debug
Name
The column names can be arbitrary, but it is best to choose clear and easy-to-understand names. Then check it Single instance only
so that only one debugger is started. And there will not be many same debuggers connected to the debug port specified later.
The following Host
writes locally127.0.0.1
, Debug Port
fill in the port number just specified above, I use it here 32312
, it is exactly the same as in the previous configuration file. Local directory
Specifies the directory where the current game-server/app.js
directory is located.
Attachment: run configuration
Of course, you may have noticed that this Configurations
includes not only debugging, but also running configuration. The running configuration method is the same as the general NodeJS program configuration method. Newcomers to NodeJS can refer to the configuration in the picture. Pay attention to the boxed content.
ready to debug
1. Start the Web and Chat servers respectively
If you have configured to run Chat and Web server, you can now select and from the configuration drop-down menu in front of the run and debug buttons, and then Chat
click Web
the [►] run button. After running, you can view the log output from the console output below the IDE, and check whether the startup of the two servers is successful. If there is a red letter, it probably means that the startup failed. You can Google or submit an Issue according to the error message prompted to find a solution.
2. Connect the debugger
Then select the debug configuration just now from the configuration drop-down menu gate study
, and then click the [☼] debug button. Check the bottom of the window Debugger
, you can see a prompt saying Connected to 127.0.0.1:32312
.
3. Set breakpoints
Since we want to debug gate
the code related to connector allocation in the server, open it in the IDE app/servers/gate/handler/gateHandler.js
. Locate to line 30 and set a breakpoint here.
practice
Open http://127.0.0.1:3001/index.html in the browser and you will see Chat of Pomelo
the login page of the title. After filling in the two-column form, click the [JOIN] button.
At this point, the page will not respond, because the server-side code is blocked by a breakpoint. And the IDE will be activated because of the debugger:
In the lower window, in addition to the necessary functions such as StepOver, StepInto, and StepOut used in conventional debugging, there are many useful tools, such as instant expression evaluation, automatic interruption when switching exceptions occur... I will not discuss it in depth here.
available chat applications
common sense
After the web server starts successfully, we can see that it prompts that it can be accessed http://127.0.0.1:3001/index.html
. But we all know that 127.0.0.1 is the local IP address, if you really want to "chat" with other people, this will not work. Because others may not be able to open 127.0.0.1:3001, even if it is opened, they cannot chat with you.
So replace 127.0.0.1 here with the address of your own computer that can be accessed by other members on the network. My address here is 192.168.1.61
. OK, I can open it in the browser http://192.168.1.61:3001/index.html
, because there is no problem with local debugging, so I also confidently told this address to my colleagues on the same network as me.
problem found
At this point a problem arises, I can log in to the chat interface myself, but no one else can.
According to the console output of other people's browsers, it can be seen that they all connected to 192.168.1.61:3014
the port and then connected again 127.0.0.1:3050
, so they were unable to log in.
And 3014
the port happens to be gate
the server, so the problem lies here, let’s continue debugging.
After some breakpoint switching, it can be found that gateHandler.queryEntry
it finally ran to the function on line 39next
.
-
var res = dispatcher.dispatch(uid, connectors);
-
next(null, {
-
code: 200,
-
host: res.host,
-
port: res.clientPort
-
});
host
It can be seen that the use of the returned content comes from res.host
, and comes from the configuration file.res
connectors
connectors
And the configuration entriesgame-server/config/servers.json
of the next connector
server can be clearly seen ."host":"127.0.0.1"
Solve the problem
I found the source, and immediately replaced everything here 127.0.0.1
with my own machine 192.168.1.61
. And restart Chat
the server.
When the colleague refreshes the visit again, he can log in.
written in the back
Careful people may find that the IP address connected by the debugger is 127.0.0.1 , but you may be thinking, since it is called "Remote Deubgger", it should be possible to connect to 127.0.0.1
other IPs, right?
I think so too. However, it cannot be done without the help of other tools.
That is, without the help of a proxy tool, there is no way to connect to 127.0.0.1
the debug port other than the address.
You can use the method and code at the end of the NodeJS official Wiki article (after Ctrl-F, search for "You probably noticed") to complete a proxy tool. In order to debug the code deployed to run on the remote server.