Codementor Events

Building a Real Time iOS multiplayer game with Swift and WebSockets (1)

Published May 15, 2018Last updated Nov 10, 2018
Building a Real Time iOS multiplayer game with Swift and WebSockets (1)

Introduction

In this tutorial, I am going to explain how one could create a multiplayer real time game using Swift and WebSockets. Swift will be used both on the client and the server. This is a four-part tutorial. The tutorial consists of the following parts:

  • Introduction and workspace setup.
  • Setup shared library project.
  • Setup server project.
  • Setup iPhone client

So what are WebSockets? Well, according to Mozilla:

WebSockets are an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.

Of course, we will not communicate from a browser, but use a native client instead.

For this example, we'll create a Tic Tac Toe game. To keep the server code simple, the server will only support a single game for two players at any time.

Code for the full project can be downloaded from my GitHub.

mp-tictactoe.png

Tools of the trade

We will use the following tools and libraries for this project:

  • Starscream: for websockets support on the client.
  • Perfect: a webserver and toolkit that is written in Swift and supports websockets.

It would have been awesome to use Perfect on the client as well, but as of yet, this is not (easily) possible. The Perfect library relies on the Swift Package Manager to be able to include it into your own projects, but for now, the Swift Package Manager is not supported for iOS. Luckily, Starscream supports Cocoapods, which is what we'll be using for the iOS app.

Workspace setup

For this game, we'll create a single workspace that includes three projects:

  • The TicTacToe server.
  • The TicTacToe iOS client.
  • A shared library for the server and client apps.

The shared library includes some shared code, mainly a Message object that is used to wrap communication between the server and client.

Setup iOS Project

The first step is to create an iOS project called TicTacToe. Use the SpriteKit game template for this project.

create-project-ios-spritekit-game-1.png
create-project-ios-spritekit-game-2.png

After the project is created, create a Podfile in the TicTacToe directory and make sure to include the Starscream cocoapod into our TicTacToe target:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.0'
use_frameworks!

target 'TicTacToe' do
  pod 'Starscream', '~> 3.0.2'
end

After running pod install, we should now have a TicTacToe workspace. The project should build without any issues.

Setup shared code project

In order to setup the shared code project, create a new macOS Cocoa Framework project called TicTacToeShared. I suggest saving the project in TicTacToe/TicTacToeShared.

create-project-macos-framework.png

Now we need to add an iOS target. I'd prefer if we'd be able to use a framework, but I couldn't get it to work - there seemed to be some linking issue when used in the iOS app. So for iOS I suggest creating a Cocoa Touch static library target.

tictactoe-shared-ios-static-library.png
create-project-tictactoe-shared-ios.png

Now make sure the Product Name in the build settings is TicTacToeShared for both targets. The product name is used for importing the library into our server and client projects.

Setup server project

Now it's time to create the server project. Inside the TicTacToe directory, we create a directory called TicTacToeServer. Download the Perfect template project into this directory as follows:

cd TicTacToeServer
git clone https://github.com/PerfectlySoft/PerfectTemplate.git .

Now, open the Package.swift file and change the name to TicTacToeServer and include the Perfect WebSockets package as a dependency:

// Generated automatically by Perfect Assistant Application
// Date: 2017-09-20 19:30:47 +0000
import PackageDescription
let package = Package(
  name: "TicTacToeServer",
  targets: [],
  dependencies: [
    .Package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", majorVersion: 3),
    .Package(url:"https://github.com/PerfectlySoft/Perfect-WebSockets.git", majorVersion: 3),
  ]
)

Run swift package generate-xcodeproj to generate an Xcode project. Make sure the project builds and runs correctly. Afterwards, the server project can now be included into the previously created workspace. You might have to edit the TicTacToeServer build settings however to make sure that the only supported platform is macOS.

tictactoe-server-supported-platforms.png

Include the shared library in the client and server projects

In order to include the shared library into our client and server project, perform the following steps:

  • Open the iOS app workspace.
  • Add the shared library project to the workspace.
  • Add the server project to the workspace.
  • Go to the build settings of the TicTacToeServer target and link the TicTacToeShared framework.

Afterwards make sure all 3 projects still compile without any issues.


Continue to part 2 of the tutorial.


Discover and read more posts from Wolfgang Schreurs
get started
post commentsBe the first to share your opinion
Alan Prochaska
a year ago

Hi Wolfgang,

I like these tutorials, but got stuck, likely because they’re four years old and platforms and components have evolved. I’m still a swift novice, so I struggled trying to follow the instructions in a newer context.

I’m using Xcode 14.0 on a 2020 MacBook Pro with 16G memory.

I had trouble with getting the server built and then incorporated into the workspace. I couldn’t tell where to create the directories and how to add in the project once created.

It was helpful to do the pod install in verbose mode. It was taking a long time, and verbose provided more feedback which assured me it was doing something.

Finally, I’m not sure I incorporated the iOS target into the workspace correctly. The layout in my Xcode didn’t look like your screenshot.

I imagine I will be able to solve all of these once I learn more about workspaces and pods, but I was hoping to use your tutorial as a way to learn hands-on.

You have probably moved on to other things by now, but if you have the time and inclination to run through it again with current components and could provide some guidance, that would be helpful.

Thank you!

Alan Prochaska
Pasadena, CA, USA

ps. If you have tutorials and sites to recommend to learn about workspaces, packages, and pods, that would be helpful, too.

Wolfgang Schreurs
a year ago

Hi,

Yes, it’s likely some dependencies have changed a lot over the last couple of years and perhaps Swift has changed as well.

Sadly my focus in the last couple of years has been mainly C# (work) and Lua (hobby). It would cost me quite a bit of time to update these tutorials. Currently I don’t really have the time.

In the meanwhile I would suggest to you to look at some other socket tutorials. If you can find a decent socket lib, I think the rest of the code should be easily adjustable. This lib seems promising for use both server side & client side: https://github.com/swiftsocket/SwiftSocket

Perhaps I’ll update this tutorial in the coming months.

Davuid Ramer
4 years ago

Hi, I’m missing the step where you create the shared code project. When I create the project there is no Cocoa framework to choose from?

Amelia_S
5 years ago

I was trying to build something similar and I came cross this code thank you so much !
https://tgw.onl/digitalocean/ https://tgw.onl/siteground/ https://tgw.onl/ipage/

Show more replies