Online Multiplayer Game with Go

When I was a junior high school student, I had a dream to build a video game. So, I learned to code. I remember the first game engines that I tried are Game Builder (if I'm not mistaken) and The Games Factory. I remember that the Game Builder uses C# for scripting. This is my first interest in computer science or software engineering.

When I was in the late stages of senior high school, the world was transformed. The era of social media began. I remember there were MySpace and Friendster. In that moment, Facebook was born and became my second influence after video games and desktop software. The features that I loved from Facebook are chatting and video uploading. That made me dream of building a social media platform.

I played some online multiplayer games when I was a kid. Such as Seal Online, Ragnarok 2, DotA, Dota 2, Counter Strike 1.6, and CS: GO. I was wondering how they work.

Today, I can demonstrate how they work. You read the title right. It is not clickbait. But please don't expect that I made an AAA game. It is just a classic game, but many people love it. So, I chose web as a client, powered by Angular. The game is Live Chess.

Many people are wondering, can we create a video game with GoLang? The answer is yes. I created an online multiplayer game with Go as a server.

The communication protocol that I chose is WebSocket. The same technique is used for messaging apps. This demonstration fulfilled my dream to build an online multiplayer game and messaging (like Messenger from Facebook or Meta).

I think the simplest way to implement WebSocket in Go is using https://github.com/gorilla/websocket. Here is the demonstration.

You can try it by yourself or inspect it. Here are the repositories:

I built the game on OpenManus, powered by Claude 3.7 Sonnet, with this prompt.

Please create a client-server program for live chess. The server must be in Go, and you should use github.com/gorilla/websocket. The client must be in (TypeScript) Angular, and the UI should be simple. The client (TypeScript) and server (Go) should be testable, so please provide the unit tests too. For example, all chess pieces should move legally, the user cannot move the pieces illegally. You can give unit tests for these chess pieces. So, both client (TypeScript) and server (Go) have similar unit tests. The server records user moves and delivers those moves or states to all clients. The server runs on port 8080 by default. The first client waits for the opponent. If the second client joins the server, the second client will be against the first client. If the third client joins the server, the third client will wait until there is a fourth client, then the third and the fourth will be in one match, and so on. The server makes sure there is no conflict between those matches. The server can deliver the states to the right match (2 clients per match). The server updates the states to all clients every second. Make sure there is no race condition. There is no database either. The server stores all states by using a map variable.

The first output wasn't perfect, but it was good enough to initialize my work. It still needs my intervention. There is a bug, the client cannot go back to the latest or previous state when the browser is refreshed or disconnected. I let the OpenManus fix it with this prompt, but it cannot. I already tried it three times, but it made no changes.

It didn't work. I started the server by doing "go run main.go". I started the client by doing "npm start" then the client started at http://localhost:4200. I started another client, then the client started at http://localhost:34973. I played the game, moved some pieces, refreshed the web browser, and then all the pieces were back to the beginning. I don't want the pieces to go back to the beginning. I want the pieces to go back to the latest state. For example, if I move a pawn from e2 to e4, refresh the page, and then that pawn is still on e4, not e2. I have seen your changes on the server. But I see you have no changes on the client side or the TypeScript Angular side. Maybe the problem is with the client. Please make sure that this time, this big problem is solved. It is crucial for online games.

Well, I didn't want to waste some tokens and make more efforts. I thought it was enough to demonstrate a use case of WebSocket.

Maybe next time I will make an online video game with beautiful graphics.

Related Articles

Comments

comments powered by Disqus