๋ ํ๋ก๊ทธ๋จ์ด ๋คํธ์ํฌ๋ฅผ ํตํด ์๋ก ํต์ ์ ์ํ ํ ์ ์๋๋ก ์์ชฝ์ ์์ฑ๋๋ ๋งํฌ์ ๋จ์.
TCP๋ ๋ ํ๋ก๊ทธ๋จ ๊ฐ ํต์ ์ด ์ฒ์ ์์๋ ๋๋ถํฐ ๋๋ ๋๊น์ง ๊ณ์ ์ฐ๊ฒฐ์ ์ ์งํ๋ ์ฐ๊ฒฐ์งํฅ(Connection oriented) ๋ฐฉ์์ด๋ค.
private static Socket socket;
socket = new Socket();
socket.connect(new InetSocketAddress("localhost","portNumber"));
private static InputStream is;
private static OutputStream os;
is = socket.getInputStream();
os = socket.getOutputStream();
byte[] byteArr = null;
String msg = "Hello Server";
byteArr = msg.getBytes("UTF-8");
os.write(byteArr);
os.flush();
System.out.println("Data Trasmitted OK!");
byteArr = new byte[512];
int readByteCount = is.read();
if(readByteCount == -1)
throw new IOException();
msg = new String(byteArr, 0, readByteCount, "UTF-8");
System.out.println("Data Received OK!");
System.out.println("Message : "+ msg);
is.close();
os.close();
socket.close();
์ฅ์
๋จ์
1๏ธโฃ Gradle์ ์ถ๊ฐํ๊ธฐ.
implementation ('com.github.nkzawa:socket.io-client:1.0.0')
2๏ธโฃ ์์ผ ์ ์ธ๊ณผ ์ด๊ธฐํ
private Socket socket;
{
try{
socket = IO.socket("http://***.****:****");
} catch (URISyntaxException ue) {
ue.printStackTrace();
}
}
3๏ธโฃ ์์ผ์ ์ฐ๊ฒฐ
socket.connect();
4๏ธโฃ Server์ ์ด๋ฒคํธ ์ก์
socket.emit("EVENT_NAME", DATA);
5๏ธโฃ Server ์ธก ์ด๋ฒคํธ๋ฅผ ์์
socket.on("EVENT_NAME", '๋ฆฌ์ค๋ ์ต๋ช
๊ตฌํ ๊ฐ์ฒด');
Emitter.Listener '๋ฆฌ์ค๋ ์ต๋ช
๊ตฌํ ๊ฐ์ฒด' = new Emitter.Listener() {
@Override
public void call(Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
// ์ด๋ฒคํธ ์์ ์ ์คํํ ๋ด์ฉ๋ค
}
});
6๏ธโฃ ์ฌ์ฉ ํ ์ฐ๊ฒฐ ํด์
protected void onDestroy() {
super.onDestroy();
socket.disconnect();
socket.off("EVENT_NAME", '๋ฆฌ์ค๋ ์ต๋ช
๊ตฌํ ๊ฐ์ฒด');
}
companion object {
private lateinit var socket : Socket
fun get(): Socket {
try {
socket = IO.socket("http://127.0.0.0:3000")
} catch (e: URISyntaxException) {
e.printStackTrace()
}
return socket
}
}
class MainActivity : AppCompatActivity() {
lateinit var Text : TextView
lateinit var lighton_btn: Button
lateinit var lightoff_btn: Button
lateinit var socket: Socket
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
lighton_btn = findViewById(R.id.lighton_button)
lightoff_btn = findViewById(R.id.lightoff_button)
Text = findViewById(R.id.message)
socket = SocketApplication.get()
Text.setText("์์ผ ์์ฑ")
socket.connect()
lighton_btn.setOnClickListener { v ->
socket.emit("lightOn")
Text.setText("Light on Emit ์ฑ๊ณต")
}
lightoff_btn.setOnClickListener { v ->
socket.emit("lightOff")
Text.setText("Light off Emit ์ฑ๊ณต")
}
}
}
class MainActivity : AppCompatActivity() {
lateinit var Text : TextView
lateinit var Receive_Text: TextView
lateinit var socket: Socket
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Text = findViewById(R.id.message)
Receive_Text = findViewById(R.id.receive_Text)
socket = SocketApplication.get()
socket.on("lightOn",light_on)
socket.on("lightOff", light_off)
socket.connect()
}
var light_on = Emitter.Listener { args ->
Text.setText("์์ผ on ์ฑ๊ณต")
Receive_Text.setText(args[0].toString())
}
var light_off = Emitter.Listener { args ->
Text.setText("์์ผ on ์ฑ๊ณต")
Receive_Text.setText(args[0].toString())
}
}