18. Player Logout
Tags: go, zinx
Published: 2023-09-26 02:11:11

### [Zinx]

[<1.Building Basic Services with Zinx Framework>](https://dev.to/aceld/1building-basic-services-with-zinx-framework-296e)
[<2. Zinx-V0.2 Simple Connection Encapsulation and Binding with Business>](https://dev.to/aceld/2-zinx-v02-simple-connection-encapsulation-and-binding-with-business-4p9g)
[<3.Design and Implementation of the Zinx Framework's Routing Module>](https://dev.to/aceld/3design-and-implementation-of-the-zinx-frameworks-routing-module-583n)
[<4.Zinx Global Configuration>](https://dev.to/aceld/4zinx-global-configuration-3jam)
[<5.Zinx Message Encapsulation Module Design and Implementation>](https://dev.to/aceld/5zinx-message-encapsulation-module-design-and-implementation-3fmc)
[<6.Design and Implementation of Zinx Multi-Router Mode>](https://dev.to/aceld/6design-and-implementation-of-zinx-multi-router-mode-19nn)
[<7. Building Zinx's Read-Write Separation Model>](https://dev.to/aceld/7-building-zinxs-read-write-separation-model-ihj)
[<8.Zinx Message Queue and Task Worker Pool Design and Implementation>](https://dev.to/aceld/8zinx-message-queue-and-task-worker-pool-design-and-implementation-12gl)
[<9. Zinx Connection Management and Property Setting>](https://dev.to/aceld/9-zinx-connection-management-and-property-setting-28ha)

### [Zinx Application - MMO Game Case Study]

[<10. Application Case Study using the Zinx Framework>](https://dev.to/aceld/10-application-case-study-using-the-zinx-framework-3ich)
[<11. MMO Online Game AOI Algorithm>](https://dev.to/aceld/11-mmo-online-game-aoi-algorithm-l7d)
[<12.Data Transmission Protocol: Protocol Buffers>](https://dev.to/aceld/12data-transmission-protocol-protocol-buffers-1jek)
[<13. MMO Game Server Application Protocol>](https://dev.to/aceld/13-mmo-game-server-application-protocol-5adl)
[<14. Building the Project and User Login>](https://dev.to/aceld/14-building-the-project-and-user-login-b7p)
[<15. World Chat System Implementation>](https://dev.to/aceld/157-world-chat-system-implementation-18li)
[<16. Online Location Information Synchronization>](https://dev.to/aceld/16-online-location-information-synchronization-3cp1)
[<17. Moving position and non-crossing grid AOI broadcasting>](https://dev.to/aceld/17-moving-position-and-non-crossing-grid-aoi-broadcasting-4nb9)
[<18. Player Logout>](https://dev.to/aceld/18-player-logout-3eln)
[<19. Movement and AOI Broadcast Across Grids>](https://dev.to/aceld/19-movement-and-aoi-broadcast-across-grids-46fe)

---
### MMO-GAME Source Code

https://github.com/aceld/zinx/tree/master/zinx_app_demo/mmo_game

---

When a user clicks on the "close" button in the client, it triggers the player logout functionality. In the case of an online player logging out, other players should see the offline player disappear from their own client interfaces. The specific process is shown in Figure 18.1.

> Figure 18.1

![Figure 18.1](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sby8m5ehb4nxr9rt9mj2.jpg)

Player Logout is achieved using the SyncPid protocol message with MsgID 201. The trigger for this process occurs just before the client disconnects from the server. By registering the player logout business logic in the hook method before the connection is lost within the Zinx framework, this functionality can be implemented.

When starting the server, register the hook method before the connection is lost, as shown below:

```go
//mmo_game/server.go

func main() {
    // Create a server instance
    s := znet.NewServer()

    // Register the hook function after client connection is established
    s.SetOnConnStart(OnConnectionAdd)

    // New: Register the hook function before client connection is lost
    s.SetOnConnStop(OnConnectionLost)

    // Register routers
    s.AddRouter(2, &api.WorldChatApi{})
    s.AddRouter(3, &api.MoveApi{})

    // Start the server
    s.Serve()
}
```
The implementation of the OnConnectionLost() method is as follows:
```go
//mmo_game/server.go

// Hook function when a client disconnects
func OnConnectionLost(conn ziface.IConnection) {
    // Get the current connection's Pid attribute
    pid, _ := conn.GetProperty("pid")

    // Get the corresponding player object based on pid
    player := core.WorldMgrObj.GetPlayerByPid(pid.(int32))

    // Trigger the player logout business logic
    if pid != nil {
        player.LostConnection()
    }

    fmt.Println("====> Player ", pid, " left =====")
}
```

OnConnectionLost() is called before the connection is lost. First, it retrieves the Pid of the current connection, then it gets the corresponding Player object, and finally, it calls the Player's LostConnection() method. Here's the implementation of the LostConnection() method for the Player:

Now we need to provide a LostConnection() method in the player module.

```go
//mmo_game/core/player.go

// Player logout
func (p *Player) LostConnection() {
    // 1. Get players in the surrounding AOI nine-grid
    players := p.GetSurroundingPlayers()

    // 2. Assemble MsgID 201 message
    msg := &pb.SyncPid{
        Pid: p.Pid,
    }

    // 3. Send the message to surrounding players
    for _, player := range players {
        player.SendMsg(201, msg)
    }

    // 4. Remove the current player from the AOI in the world manager
    WorldMgrObj.AoiMgr.RemoveFromGridByPos(int(p.Pid), p.X, p.Z)
    WorldMgrObj.RemovePlayerByPid(p.Pid)
}
```

he LostConnection() method of the Player first retrieves information about all surrounding players, then sends the SyncPid protocol message with MsgID 201 to each online player's client, and finally removes the current player's information from the world manager.

Next, start the server and launch two clients to test the results, as shown in Figure 18.2.

> Figure 18.2

![Figure 18.2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q2apitwcqhnh5hmx9pqw.png)

In the image, we have Player_4 and Player_3. Before Player_4 clicks the "Exit" button, both players can see each other in their respective client interfaces, as shown in Figure 21.19. However, after Player_4 clicks the "Exit" button, the result is as shown in Figure 18.3.

> Figure 18.3

![Figure 18.3](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/okkn492jypeckkudllia.png)

From the program's behavior, it's evident that Player_3 can no longer see the character information of Player_4 in their field of view. This indicates that when one client logs out, the player on the map of the other client gets removed.

---
### MMO-GAME Source Code
https://github.com/aceld/zinx/tree/master/zinx_app_demo/mmo_game

---
### [Zinx]

[<1.Building Basic Services with Zinx Framework>](https://dev.to/aceld/1building-basic-services-with-zinx-framework-296e)
[<2. Zinx-V0.2 Simple Connection Encapsulation and Binding with Business>](https://dev.to/aceld/2-zinx-v02-simple-connection-encapsulation-and-binding-with-business-4p9g)
[<3.Design and Implementation of the Zinx Framework's Routing Module>](https://dev.to/aceld/3design-and-implementation-of-the-zinx-frameworks-routing-module-583n)
[<4.Zinx Global Configuration>](https://dev.to/aceld/4zinx-global-configuration-3jam)
[<5.Zinx Message Encapsulation Module Design and Implementation>](https://dev.to/aceld/5zinx-message-encapsulation-module-design-and-implementation-3fmc)
[<6.Design and Implementation of Zinx Multi-Router Mode>](https://dev.to/aceld/6design-and-implementation-of-zinx-multi-router-mode-19nn)
[<7. Building Zinx's Read-Write Separation Model>](https://dev.to/aceld/7-building-zinxs-read-write-separation-model-ihj)
[<8.Zinx Message Queue and Task Worker Pool Design and Implementation>](https://dev.to/aceld/8zinx-message-queue-and-task-worker-pool-design-and-implementation-12gl)
[<9. Zinx Connection Management and Property Setting>](https://dev.to/aceld/9-zinx-connection-management-and-property-setting-28ha)

### [Zinx Application - MMO Game Case Study]

[<10. Application Case Study using the Zinx Framework>](https://dev.to/aceld/10-application-case-study-using-the-zinx-framework-3ich)
[<11. MMO Online Game AOI Algorithm>](https://dev.to/aceld/11-mmo-online-game-aoi-algorithm-l7d)
[<12.Data Transmission Protocol: Protocol Buffers>](https://dev.to/aceld/12data-transmission-protocol-protocol-buffers-1jek)
[<13. MMO Game Server Application Protocol>](https://dev.to/aceld/13-mmo-game-server-application-protocol-5adl)
[<14. Building the Project and User Login>](https://dev.to/aceld/14-building-the-project-and-user-login-b7p)
[<15. World Chat System Implementation>](https://dev.to/aceld/157-world-chat-system-implementation-18li)
[<16. Online Location Information Synchronization>](https://dev.to/aceld/16-online-location-information-synchronization-3cp1)
[<17. Moving position and non-crossing grid AOI broadcasting>](https://dev.to/aceld/17-moving-position-and-non-crossing-grid-aoi-broadcasting-4nb9)
[<18. Player Logout>](https://dev.to/aceld/18-player-logout-3eln)
[<19. Movement and AOI Broadcast Across Grids>](https://dev.to/aceld/19-movement-and-aoi-broadcast-across-grids-46fe)

---
Author:
discord: https://discord.gg/xQ8Xxfyfcz
zinx:   https://github.com/aceld/zinx
github: https://github.com/aceld
aceld's home: https://yuque.com/aceld