Browse Source

Started creating the server-client communication channel.

pull/8/head
jrtechs 6 years ago
parent
commit
e384df3bd4
3 changed files with 166 additions and 0 deletions
  1. +26
    -0
      src/main/java/net/jrtechs/www/client/index.html
  2. +31
    -0
      src/main/java/net/jrtechs/www/server/Client.java
  3. +109
    -0
      src/main/java/net/jrtechs/www/server/Server.java

+ 26
- 0
src/main/java/net/jrtechs/www/client/index.html View File

@ -0,0 +1,26 @@
<!doctype html>
<html>
<head>
<script>
var connection = new WebSocket('ws://127.0.0.1:4444');
connection.onopen = function () {
console.log('Connected!');
connection.send('Ping'); // Send the message 'Ping' to the server
};
// Log errors
connection.onerror = function (error) {
console.log('WebSocket Error ' + error);
};
// Log messages from the server
connection.onmessage = function (e) {
console.log('Server: ' + e.data);
};
</script>
</head>
<body>
<ul id='messages'></ul>
</body>
</html>

+ 31
- 0
src/main/java/net/jrtechs/www/server/Client.java View File

@ -0,0 +1,31 @@
package net.jrtechs.www.server;
import org.java_websocket.WebSocket;
public class Client extends Thread
{
private WebSocket client;
public Client(WebSocket client)
{
this.client = client;
}
public void recievedMessage(String message)
{
}
public WebSocket getSocket()
{
return this.client;
}
@Override
public void run() {
}
}

+ 109
- 0
src/main/java/net/jrtechs/www/server/Server.java View File

@ -0,0 +1,109 @@
package net.jrtechs.www.server;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import java.net.InetSocketAddress;
import java.util.HashSet;
import java.util.Set;
/**
* Socket server which listens for clients
*
* @author Jeffery Russell 5-26-18
*/
public class Server extends WebSocketServer
{
/** port to listen on **/
private static int TCP_PORT = 4444;
/** clients connected to the server **/
private Set<Client> clients;
/**
* Initializes the server and creates an empty set of clients
*/
public Server()
{
super(new InetSocketAddress(TCP_PORT));
clients = new HashSet<>();
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake)
{
clients.add(new Client(conn));
System.out.println("New connection from " +
conn.getRemoteSocketAddress().getAddress().getHostAddress());
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote)
{
this.removeClient(conn);
System.out.println("Closed connection to " +
conn.getRemoteSocketAddress().getAddress().getHostAddress());
}
@Override
public void onMessage(WebSocket conn, String message)
{
System.out.println("Message from client: " + message);
for (Client client : clients)
{
if(client.getSocket() == conn)
{
client.recievedMessage(message);
}
}
}
@Override
public void onError(WebSocket conn, Exception ex)
{
//ex.printStackTrace();
if (conn != null)
{
clients.remove(conn);
// do some thing if required
}
System.out.println("ERROR from " + conn.getRemoteSocketAddress()
.getAddress().getHostAddress());
}
/**
* Removes a client from the main list of clients
* based on the websocket that needs to be removed.
*
* @param conn
*/
public void removeClient(WebSocket conn)
{
for(Client c: clients)
{
if(c.getSocket() == conn)
{
this.clients.remove(c);
c.stop();
}
}
}
/**
* Starts the web socket server
*
* @param args
*/
public static void main(String[] args)
{
new Server().start();
}
}

Loading…
Cancel
Save