#How do I create a LAN server for a game that I can connect to on my phone?

87 messages · Page 1 of 1 (latest)

normal bane
#

I'm trying to make a version of the game Inscryption for my friends and I to play when we hang out. I'm currently using PhaserJS for this project and I want to set up a local LAN server that we can all connect to via our phones. I have never done anything related to netowrking, websockets, HTTP, or anything of the sort. I'm using Vite as my framework, but I dont know if it has anything that could help me out with what I'm trying to do.

Heres my server code, but I dont really know how to run it in a way where I can connect to it on my phone and then be able to interact with the game.

import express from "express";
import path from 'path'

const app = express()
const port = 433
const localIP = '192.168.0.238'

app.use(express.static(path.join(__dirname, 'client')))

app.listen(port, localIP, () => {
    console.log(`Server is running at http://${localIP}:${port}`)
})
#

Also I mostly use ChatGPT for code help because its fast and easy, so theres a good chance that it gave me some garbage code

desert marlin
#

if you're hosting onto your local ip, then you can just connect to the same network and use that local ip

#

but the local ip can change upon reconnecting to the network

normal bane
#

Im trying to host it on my machine, but have it publicly available on the entire wifi network if that makes sense

desert marlin
#

yeah that's kinda the normal thing

tranquil cradle
#

What capabilities does your server need? It seems to me that all you want is to just serve some static files.

normal bane
#

yea so i want to serve some static files, and then some objects that contain player input data, yk? Like what card is played, how much damage someone takes, who wins a round, etc

tranquil cradle
#

If that (only serving static files) is the case, you don't need to write a single line of code, and can just use an existing server and that's all. Vite has it (vite preview) or you can use something like http-server.

desert marlin
#

seems like this is doing some multiplayer stuff, would require a server to handle synchronization, no?

normal bane
desert marlin
#

synchronization of data is still synchronization

#

if a player's actions can affect another player, that's synchronization

normal bane
#

well sure

#

is there any simple way of handling synchronization?

desert marlin
#

uh, a server...

#

i guess http polling might require the least setup

#

ws would be more reliable but it'd require getting that up

normal bane
#

ive spent so much time trying to figure out ws

desert marlin
#

im not sure if p2p would be an option with websites

normal bane
#

idk if p2p is the best option for LAN

tranquil cradle
#

So you want more than a static server then, ignore what I said.

normal bane
#

does vite have anything similar to http-server but for dynamic sites?

tranquil cradle
#

No

normal bane
#

bummer

desert marlin
#

http-server isn't a vite thing, it's just a server thing

tranquil cradle
#

Vite is not for making production servers, it's just a dev tool.

#

I only suggested it because you seemed like you only wanted to serve static files.

desert marlin
normal bane
#

yea i gotcha

tranquil cradle
desert marlin
#

Optimized Build
Pre-configured Rollup build with multi-page and library mode support.

desert marlin
tranquil cradle
#

Building is taking some source code and produce artifacts, serving is answering HTTP requests with responses.

#

Vite does the former (and also a dev server), but it's not a production server.

desert marlin
#

oh, i misread what you said

desert marlin
normal bane
#

so if im not using http-server to serve static files, would i want to use websocket to serve the files alongside other use data?

desert marlin
#

websocket doesn't serve files, it's just for 2-way communication

normal bane
#

im confused now

#

if i cant use http-server or websocket, then what do i use?

#

do i use them both at the same time?

desert marlin
#

i think you're confusing a few things here

normal bane
#

i bet i am

desert marlin
#

so in the end you should have 2 parts: the client (the website) and the server (the part handling gameplay logic)
vite is part of your client, it is the build tool. it only has static files, which the browser will request via http. you need something to handle those requests to serve the static files, like http-server or serve

#

the client will then communicate with the server to determine what to do (what to show the user), and tell the server what to do (what the user inputs)

#

the client-server communication is typically 2-way with games, so websocket is often used to facilitate the communication

#

websocket, like http, is a protocol, not some specific library

#

additionally, websocket requires an http request to initiate a connection, so you can't use just ws

tranquil cradle
#

Yeah if you need to write a multiplayer game, there's really no magic bullet that will do all the work for you, you really just need to write your own logic.
Libraries can do the serving files/handling WebSocket connection part though. You don't necessarily need two servers (one for serving files one for WS), one server can do both, but it might be convenient to do it with two anyways.

normal bane
#

hmm

#

im starting to understand

#

doesnt npm run dev --host do a lot of what im trying to do?

desert marlin
#

assuming you're referring to the dev script that vite provides in its create package, then well, it'd be a dev server rather than a production server

#

but tbh for small scales it'll probably be fine

normal bane
#

is using dev bad or something?

desert marlin
#

in terms of what?

#

it'd be bad for hosting a large scale production server

#

as its name says, it's intended for hosting a development server

tranquil cradle
#

And also, all it does is just serving static files. You said you need multiplayer logic, so just serving static files is not enough.

desert marlin
#

it'd handle serving the client though

tranquil cradle
#

Even then you shouldn't use npm run dev because presumably it calls into vite dev, which doesn't actually serve the built files but your source files, which are unoptimized and have stuffs like HMR built in.

desert marlin
#

oh, stuff like hmr would be some overhead, huh.

tranquil cradle
#

If you really want to use Vite to serve your files, use vite preview, but even that's discouraged by Vite.

normal bane
#

dang this is all alot harder than i was expecting it to be

gloomy cedar
#

totally not helpful to you at all, but inscryption sounds pretty fun

normal bane
normal bane
#

can node accomplish waht i need?

gloomy cedar
#

nodejs would most likely be the runtime for the server code you wrote (there are other options, but they aren't as popular)

#

just like your frontend JS is executed by a web browser, your backend JS is executed by node

tranquil cradle
normal bane
#

yea i could deal with that

gloomy cedar
#

(sorry i didn't read the whole scrollback, was assuming the code you shared in your first message as a starting point)

normal bane
#

that code wouldnt run, i ended up deleting it

#

i really dont know what to do anymore

#

my server.js script wont run

gloomy cedar
#

then the next step would probably be learning about npm and package.json and how to install/use third-party dependencies (like express in your original code)

normal bane
#

no i know how to install and use dependencies. i just havent ever done any kind of project that as required me to interract with node, or actually build my project

#

i always just go to my project folder and type npm run dev and then it works

gloomy cedar
#

you've only ever written client-side code before?

normal bane
#

yea pretty much

gloomy cedar
#

i would still suggest getting that little hello world node server up and running in isolation then. just to get a basic understanding of the fundamental stuff that needs to happen

#

forget everything you know, just follow the instructions in the link i sent in a new directory