-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathServerWS.java
147 lines (130 loc) · 4.2 KB
/
ServerWS.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.asambhava.groupchatws;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import org.java_websocket.WebSocket;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author sarthak_z9
*/
public class ServerWS extends WebSocketServer{
private static int TCP_PORT = 9000;
private Set<WebSocket> conns;
private Socket socket;
public ServerWS(int port) {
super(new InetSocketAddress(port));
TCP_PORT = port;
conns = new HashSet<>();
//connecting with messaging service
socket = null;
try
{
socket = new Socket("127.0.0.1", 8888);
System.out.println("Instance "+TCP_PORT+" Connected to messaging service");
MessageFromOtherInstances mfoi = new MessageFromOtherInstances(this, socket);
Thread mfoiT = new Thread(mfoi);
mfoiT.start();
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("Hello");
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
conns.add(conn);
System.out.println("New connection from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
conns.remove(conn);
System.out.println("Closed connection to " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
}
@Override
public void onMessage(WebSocket conn, String message) {
if(message.equals("FTI Pass Alpha destroy")){
System.out.println("Failing this instance as REQUESTED");
int x=0;
x=x/x; // haa haa haa
}
System.out.println("Message from client: " + message);
DataOutputStream dos;
try {
dos = new DataOutputStream(socket.getOutputStream());
dos.writeUTF(message);
} catch (IOException ex) {
Logger.getLogger(ServerWS.class.getName()).log(Level.SEVERE, null, ex);
}
for (WebSocket sock : conns) {
if(sock!=conn)
sock.send(message);
}
}
@Override
public void onError(WebSocket conn, Exception ex) {
//ex.printStackTrace();
if (conn != null) {
conns.remove(conn);
// do some thing if required
}
System.out.println("ERROR from " + conn.getRemoteSocketAddress().getAddress().getHostAddress());
}
public void HandleMessageFOI(String m){
for (WebSocket sock : conns) {
sock.send(m);
}
}
public static void main(String[] args){
ServerWS server;
if(System.getenv("PORT")!=null){
System.out.println(System.getenv("PORT")+"oooopsss");
server = new ServerWS(Integer.parseInt(System.getenv("PORT")));
}
else
{
System.out.println("null hai ye");
server = new ServerWS(9000);
}
server.start();
}
}
class MessageFromOtherInstances extends Thread{
ServerWS server;
Socket socket;
public MessageFromOtherInstances(ServerWS ser,Socket s){
server = ser;
socket = s;
}
@Override
public void run()
{
while (true)
{
try {
// receive the message from client
DataInputStream dis = new DataInputStream(socket.getInputStream());
String rr = dis.readUTF();
System.out.println("coming to instance "+rr);
server.HandleMessageFOI(rr);
} catch (IOException e) {
e.printStackTrace();
if(socket==null)
break;
}
}
}
}