Add networklistener
This commit is contained in:
parent
52c5fb6048
commit
18821b065f
@ -11,8 +11,8 @@ public class GameClient implements Constants {
|
|||||||
|
|
||||||
private final SocketHelper helper;
|
private final SocketHelper helper;
|
||||||
|
|
||||||
public GameClient(String host) throws IOException {
|
public GameClient(String host, NetworkListener listener) throws IOException {
|
||||||
helper = new SocketHelper(new Socket(host, PORT));
|
helper = new SocketHelper(new Socket(host, PORT), listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocketHelper getHelper() {
|
public SocketHelper getHelper() {
|
||||||
|
@ -12,9 +12,9 @@ public class GameServer implements Constants {
|
|||||||
private final ServerSocket serverSocket;
|
private final ServerSocket serverSocket;
|
||||||
private final SocketHelper helper;
|
private final SocketHelper helper;
|
||||||
|
|
||||||
public GameServer() throws IOException {
|
public GameServer(NetworkListener listener) throws IOException {
|
||||||
serverSocket = new ServerSocket(PORT);
|
serverSocket = new ServerSocket(PORT);
|
||||||
helper = new SocketHelper(serverSocket.accept());
|
helper = new SocketHelper(serverSocket.accept(), listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public SocketHelper getHelper() {
|
public SocketHelper getHelper() {
|
||||||
|
13
src/com/annimon/turrets/NetworkListener.java
Normal file
13
src/com/annimon/turrets/NetworkListener.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.annimon.turrets;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public interface NetworkListener {
|
||||||
|
|
||||||
|
public static final int
|
||||||
|
ON_CONNECT = 1;
|
||||||
|
|
||||||
|
public void onStatusChanged(int status, Object data);
|
||||||
|
}
|
@ -9,18 +9,26 @@ import java.net.Socket;
|
|||||||
*
|
*
|
||||||
* @author aNNiMON
|
* @author aNNiMON
|
||||||
*/
|
*/
|
||||||
public class SocketHelper {
|
public class SocketHelper extends Thread {
|
||||||
|
|
||||||
private final Socket socket;
|
private final Socket socket;
|
||||||
private final DataInputStream dis;
|
private final DataInputStream dis;
|
||||||
private final DataOutputStream dos;
|
private final DataOutputStream dos;
|
||||||
|
|
||||||
public SocketHelper(Socket socket) throws IOException {
|
private final NetworkListener listener;
|
||||||
|
|
||||||
|
public SocketHelper(Socket socket, NetworkListener listener) throws IOException {
|
||||||
this.socket = socket;
|
this.socket = socket;
|
||||||
|
this.listener = listener;
|
||||||
dis = new DataInputStream(socket.getInputStream());
|
dis = new DataInputStream(socket.getInputStream());
|
||||||
dos = new DataOutputStream(socket.getOutputStream());
|
dos = new DataOutputStream(socket.getOutputStream());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
listener.onStatusChanged(NetworkListener.ON_CONNECT, null);
|
||||||
|
}
|
||||||
|
|
||||||
public void close() throws IOException {
|
public void close() throws IOException {
|
||||||
if (dis != null) dis.close();
|
if (dis != null) dis.close();
|
||||||
if (dos != null) dos.close();
|
if (dos != null) dos.close();
|
||||||
|
Loading…
Reference in New Issue
Block a user