-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Realtime WebSockets Event Server #261
Open
DecryptingElectrons
wants to merge
19
commits into
xspanger3770:develop
Choose a base branch
from
DecryptingElectrons:Realtime-Websockets-server
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
2105bb7
first phases of RTWS server
18de005
Broadcasting working
5e97580
Cleanup code
adab3be
cleanup
3043bc2
improved handler chain and added better ping mechanism
230249a
cleanup
a20bfdf
fixed ping executors
3528c15
Added connection limits to Settings
db46092
removed debug code
da8f05f
cleanup
742bfdd
fixed PHASES
2905bc2
moved singletons to be used from a single class. fixed other minor is…
0e57a8c
changed hashtable to hashmap
22e9d51
synchronized added to clientDisconnected
2680e4d
changed getClients to return the HashMap instead of making a list copy
d77e14f
added a overload test written in JS
e89c51e
Merge pull request #23 from xspanger3770/develop
DecryptingElectrons 1ad1ceb
Merge pull request #24 from DecryptingElectrons/dev
DecryptingElectrons 5e93918
added debugging example
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"name": "ws_overload_test", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "ws_overload_test.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"ws": "^1.1.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#check if Node is installed | ||
if ! [ -x "$(command -v node)" ]; then | ||
echo -e '\033[0;31mError: Node.js is not installed.\033[0m' >&2 | ||
exit 1 | ||
fi | ||
|
||
#check if ./node_modules/ws exists | ||
if [ ! -d "./node_modules/ws" ]; then | ||
npm install | ||
fi | ||
|
||
|
||
#amount of processes | ||
#connections per process | ||
#address | ||
|
||
node ws_overload_test.js 20 1000 "ws://localhost:8081/realtime_events/v1" |
36 changes: 36 additions & 0 deletions
36
.construction_tools/NodeJS_ws_overload_test/ws_overload_test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const cluster = require('cluster'); | ||
const WebSocket = require('ws') | ||
|
||
const numProcesses = process.argv[2]; // Get number of processes from argument | ||
const numConnectionsPerProcess = process.argv[3]; // Get number of connections per process from argument | ||
const serverAddress = process.argv[4]; // Get server address from argument | ||
const futureTime = Date.now() + 5000; // Set time 5 seconds in the future | ||
|
||
|
||
if (cluster.isMaster) { | ||
console.log(`Master process started (PID: ${process.pid})`); | ||
console.log(`Starting ${numProcesses} processes, each creating ${numConnectionsPerProcess} connections to ${serverAddress}`); | ||
|
||
// Spawn worker processes | ||
for (let i = 0; i < numProcesses; i++) { | ||
cluster.fork(); | ||
} | ||
|
||
cluster.on('exit', (worker, code, signal) => { | ||
console.log(`Worker process (PID: ${worker.process.pid}) exited with code: ${code}, signal: ${signal}`); | ||
}); | ||
} | ||
|
||
else { | ||
console.log(`Worker process started (PID: ${process.pid})`); | ||
|
||
// Wait until future time before creating connections | ||
while (Date.now() < futureTime) {} | ||
|
||
// Create WebSocket connections | ||
for (let i = 0; i < numConnectionsPerProcess; i++) { | ||
const ws = new WebSocket(serverAddress); | ||
ws.on('open', () => console.log(`Worker ${process.pid} - Connection ${i+1} opened`)); | ||
ws.on('error', (error) => console.error(`Worker ${process.pid} - Connection ${i+1} error:`, error)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,3 +53,4 @@ science/ | |
.idea/ | ||
/screenshots/ | ||
latest/ | ||
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
GlobalQuakeServer/src/main/java/gqserver/websocketserver/Client.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package gqserver.websocketserver; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.net.SocketAddress; | ||
import java.time.Duration; | ||
import java.util.concurrent.Future; | ||
|
||
import org.eclipse.jetty.websocket.api.Session; | ||
|
||
import org.tinylog.Logger; | ||
|
||
public class Client { | ||
private Session session; | ||
private String ip; | ||
private String uniqueID; | ||
private Future<?> pingFuture; | ||
private Long lastMessageTime = 0L; | ||
|
||
private static Duration pingInterval = Duration.ofSeconds(25); | ||
|
||
/** | ||
* Create a new client object from a Jetty WebSocket session | ||
* @param session | ||
*/ | ||
public Client(Session session) { | ||
this.session = session; | ||
|
||
|
||
SocketAddress remoteAddress = session.getRemoteAddress(); | ||
//If the remote address is null, close the connection. Might happen.. idk | ||
if(remoteAddress == null) { | ||
Logger.error("A critical error occurred while trying to get the remote address for a new client"); | ||
session.close(0, "No remote address"); | ||
return; | ||
} | ||
|
||
InetSocketAddress inetAddress = (InetSocketAddress) remoteAddress; | ||
|
||
ip = inetAddress.getAddress().getHostAddress(); | ||
uniqueID = ip + ":" + inetAddress.getPort(); | ||
|
||
pingFuture = WebSocketEventServer.getInstance().getClientsHandler().getPingExecutor().scheduleAtFixedRate(this::pingThread, pingInterval.toMillis(), pingInterval.toMillis(), java.util.concurrent.TimeUnit.MILLISECONDS); | ||
} | ||
|
||
|
||
private void pingThread(){ | ||
if(!isConnected()) { | ||
pingFuture.cancel(true); | ||
return; | ||
} | ||
|
||
Long timeSinceLastMessage = System.currentTimeMillis() - lastMessageTime; | ||
|
||
//If the time since the last message is less than a third of the ping interval, don't send a ping | ||
if(timeSinceLastMessage<pingInterval.toMillis()/3){ | ||
DecryptingElectrons marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return; | ||
} | ||
|
||
try { | ||
session.getRemote().sendPing(null); | ||
} catch (Exception e) { | ||
session.close(); | ||
} | ||
|
||
lastMessageTime = System.currentTimeMillis(); | ||
} | ||
|
||
public void sendString(String message) throws IOException { | ||
session.getRemote().sendString(message); | ||
updateLastMessageTime(); | ||
} | ||
|
||
public boolean isConnected() { | ||
return session.isOpen(); | ||
} | ||
|
||
public void disconnectEvent() { | ||
WebSocketEventServer.getInstance().getClientsHandler().clientDisconnected(this.getUniqueID()); | ||
} | ||
|
||
public void updateLastMessageTime() { | ||
lastMessageTime = System.currentTimeMillis(); | ||
} | ||
|
||
public String getIP() { | ||
return ip; | ||
} | ||
|
||
public Session getSession() { | ||
return session; | ||
} | ||
|
||
public String getUniqueID() { | ||
return uniqueID; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK