CS/Android(Java & Kotlin)
Android Studio - 6. SocketIO
bonggang
2020. 6. 29. 11:44
0. dependency 추가
implementation 'com.github.nkzawa:socket.io-client:0.6.0'
1. 소켓 생성
lateinit var mSocket: com.github.nkzawa.socketio.client.Socket
var ip = "서버 주소"
var port ="포트번호"
fun connectSocket(){
try{
mSocket = IO.socket("http://"+ip+port)
}catch (e:URISyntaxException){
Log.d("socket error log", "socket error: " + e.toString())
}
mSocket.connect()
}
2. 통신 함수
fun commuString(infos:JSONObject, name:String){
mSocket!!.emit(name, infos) //name 이벤트 전송 요청, infos 값 전송
mSocket!!.on(name, Emitter.Listener {
var result = JSONObject(it[0].toString())
Log.d("result", result);
})
}