Skip to content

Commit 4b0ad62

Browse files
committed
[U] Custom Port Feature Extension & Fixes
[U] Update the compatibility of changing lobby and relay port (WIP on finding better solution) [F] Forgot to add the server side online count log that mentioned in the last commit [+] USAGE.md for the port changing tutorial
1 parent 98387d5 commit 4b0ad62

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

USAGE.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# WorldLink Server Usage Guide
2+
3+
## Port Configuration
4+
5+
The WorldLink server uses two ports that can be configured in multiple ways:
6+
7+
### **Default Ports:**
8+
- **Lobby Port**: 20100 (HTTP API server)
9+
- **Relay Port**: 20101 (Game communication server)
10+
- (Change default ports in Application.kt and FutariLobby.kt, I made these changes cuz the cloud provider is not really giving me 20100 and 20101)
11+
- (NOT the best solution, still trying to make it pretty, but it is what it is for now)
12+
13+
## Configuration Methods
14+
15+
### **1. Command Line Arguments (Recommended)**
16+
17+
```bash
18+
# Start with custom ports
19+
java -jar build/libs/worldlinkd.jar --lobby-port 20100 --relay-port 20101
20+
21+
# Start with only lobby port (relay uses default)
22+
java -jar build/libs/worldlinkd.jar --lobby-port 20100
23+
24+
# Start with only relay port (lobby uses default)
25+
java -jar build/libs/worldlinkd.jar --relay-port 20101
26+
```
27+
28+
### **2. Environment Variables**
29+
30+
```bash
31+
# Set environment variables
32+
export LOBBY_PORT=20100
33+
export RELAY_PORT=20101
34+
35+
# Start the server
36+
java -jar build/libs/worldlinkd.jar
37+
```
38+
39+
### **3. Windows Environment Variables**
40+
41+
```cmd
42+
# Set environment variables
43+
set LOBBY_PORT=20100
44+
set RELAY_PORT=20101
45+
46+
# Start the server
47+
java -jar build\libs\worldlinkd.jar
48+
```
49+
50+
## Priority Order
51+
52+
1. **Command line arguments** (highest priority)
53+
2. **Environment variables** (medium priority)
54+
3. **Default values** (lowest priority)
55+
56+
## Examples
57+
58+
### **Example 1: Quick Test with Default Ports**
59+
```bash
60+
java -jar build/libs/worldlinkd.jar
61+
```
62+
63+
### **Example 2: Custom Ports for Cloud Server**
64+
```bash
65+
java -jar build/libs/worldlinkd.jar --lobby-port 11451 --relay-port 19198
66+
```
67+
68+
### **Example 4: Using Environment Variables**
69+
```bash
70+
export LOBBY_PORT=20100
71+
export RELAY_PORT=20101
72+
java -jar build/libs/worldlinkd.jar
73+
```
74+
75+
## Client Configuration
76+
77+
After starting the server with custom ports, update your `WorldLink.toml`:
78+
79+
```toml
80+
# Point to your server with the lobby port
81+
LobbyUrl="http://YOUR_SERVER_IP:YOUR_LOBBY_PORT"
82+
```
83+
84+
## Troubleshooting
85+
86+
### **Port Already in Use**
87+
If you get a "port already in use" error:
88+
```bash
89+
# Check what's using the port
90+
netstat -an | grep :8080
91+
92+
# Use a different port
93+
java -jar build/libs/worldlinkd.jar --lobby-port 8081 --relay-port 8082
94+
```
95+
96+
### **Firewall Issues**
97+
Make sure your firewall allows:
98+
- TCP port for lobby server
99+
- TCP port for relay server
100+
- UDP ports (dynamic, for peer-to-peer)
101+
102+
### **Client Can't Connect**
103+
1. Verify the server is running: `netstat -an | grep :YOUR_LOBBY_PORT`
104+
2. Check your `WorldLink.toml` configuration
105+
3. Ensure the lobby port in the URL matches your server configuration

src/main/kotlin/Application.kt

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,33 @@ import io.ktor.server.plugins.statuspages.*
1111
import io.ktor.server.response.*
1212

1313
fun main(args: Array<String>) {
14-
embeddedServer(Netty, port = 20100, module = Application::module).start()
15-
FutariRelay().start()
14+
// Parse command line arguments with default port
15+
val lobbyPort = parsePort(args, "--lobby-port", "LOBBY_PORT", 20100)
16+
val relayPort = parsePort(args, "--relay-port", "RELAY_PORT", 20101)
17+
18+
println("=== WorldLink Server Configuration ===")
19+
println("Lobby Port (HTTP API): $lobbyPort")
20+
println("Relay Port (Game Communication): $relayPort")
21+
println("=====================================")
22+
23+
embeddedServer(Netty, port = lobbyPort, module = Application::module).start()
24+
FutariRelay(relayPort).start()
25+
}
26+
27+
/**
28+
* Parse port from command line arguments or environment variables
29+
* Priority: Command line args > Environment variables > Default value
30+
*/
31+
private fun parsePort(args: Array<String>, argName: String, envName: String, defaultPort: Int): Int {
32+
// Check command line arguments first
33+
for (i in args.indices) {
34+
if (args[i] == argName && i + 1 < args.size) {
35+
return args[i + 1].toIntOrNull() ?: defaultPort
36+
}
37+
}
38+
39+
// Fall back to environment variable
40+
return System.getenv(envName)?.toIntOrNull() ?: defaultPort
1641
}
1742

1843
fun Application.module() {

src/main/kotlin/FutariLobby.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,10 @@ fun Application.configureRouting() = routing {
8181
}
8282

8383
get("/info") {
84+
val relayPort = System.getenv("RELAY_PORT")?.toIntOrNull() ?: 20101
8485
mapOf(
8586
"relayHost" to (hostOverride ?: call.request.local.serverHost),
86-
"relayPort" to 20101
87+
"relayPort" to relayPort
8788
).ok()
8889
}
8990

src/main/kotlin/FutariRelay.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class FutariRelay(private val port: Int = 20101) {
158158
clients[client.stubIp] = client
159159
handler = clients[client.stubIp]
160160
log.info("[+] Client registered: ${socket.remoteSocketAddress} -> $id")
161+
log.info("[=] Online now: ${clients.size}")
161162

162163
// Send back the version
163164
handler?.send(ctlMsg(Command.CTL_START, "version=$PROTO_VERSION"))
@@ -177,6 +178,7 @@ class FutariRelay(private val port: Int = 20101) {
177178
handler?.stubIp?.let { clients.remove(it) }
178179
socket.close()
179180
log.info("[-] Client disconnected: ${handler?.clientKey}")
181+
log.info("[=] Online now: ${clients.size}")
180182
}
181183
}
182184
}

0 commit comments

Comments
 (0)