WebStorm debugging Nodejs Pomelo application engineering environment configuration

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=5858parameters to the running command, which can make the NodeJS program listen to the local 5858port and enable the debugging mode.

  • Secondly, the operating principle of Pomelo is: pomelo startwhen the server is started master, it first reads the configuration file, and then starts game-server/config/servers.jsoneach server process configured in the file. idAmong them, the , host, and so on you see portare the startup parameters of the sub-server, and these parameters will be placed app.jsbehind the startup command line.

  • Again, the designers of Pomelo have initially considered the easy way to add parameters directly before and nodeafter app.js: in the server configuration file, "args": " 你想要的参数 "after the configuration entry is inserted, it can run with the parameters you specify node.

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:

Streamlined default server configuration

2. Modify and save

We gatebecame 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 getethe server's config group look like this:

gate server debugging configuration

When the server is started again, masterthe server will read the new configuration and run gate-server-1the server with this configuration.

The process of the server will listen to 32312the port so that the debugger can connect. Setting the port higher is to reduce the possibility of port conflicts. ADDRINUSEDuring the development process, if you find an error (the port is already occupied) after startup , please use pomelo kill --forcethe 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....

Open the Edit Configurations dialog

2. Add a newNode.js Remote Debug

Add Node.js Remote Debug configuration

gate study debugger configuration

NameThe column names can be arbitrary, but it is best to choose clear and easy-to-understand names. Then check it Single instance onlyso that only one debugger is started. And there will not be many same debuggers connected to the debug port specified later.

The following Hostwrites locally127.0.0.1 , Debug Portfill in the port number just specified above, I use it here 32312, it is exactly the same as in the previous configuration file. Local directorySpecifies the directory where the current game-server/app.jsdirectory is located.

Attachment: run configuration

Of course, you may have noticed that this Configurationsincludes 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.

Game-server and web-server running configuration examples

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 Chatclick Webthe [►] 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.

Run and debug configuration selection

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.

debugger connected

3. Set breakpoints

Since we want to debug gatethe 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.

Breakpoints in gateHandler.js

practice

Open http://127.0.0.1:3001/index.html in the browser   and you will see  Chat of Pomelothe login page of the title. After filling in the two-column form, click the [JOIN] button.

Chat Of Pomelo Login Page

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:

start debugging

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:3014the port and then connected again 127.0.0.1:3050, so they were unable to log in.

And 3014the port happens to be gatethe server, so the problem lies here, let’s continue debugging.

After some breakpoint switching, it can be found that gateHandler.queryEntryit finally ran to  the function  on  line 39next .

 
 
  1. var res = dispatcher.dispatch(uid, connectors);

  2. next(null, {

  3. code: 200,

  4. host: res.host,

  5. port: res.clientPort

  6. });

host It can be seen that the use of the returned content  comes from res.host, and comes from the configuration file.resconnectorsconnectors

And the configuration entriesgame-server/config/servers.json of the next connectorserver   can be clearly seen ."host":"127.0.0.1"

Solve the problem

I found the source, and immediately replaced everything here 127.0.0.1with my own machine 192.168.1.61. And restart Chatthe 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.1other 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.1the 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.

Guess you like

Origin blog.csdn.net/qq_25062671/article/details/122444900