Kotlin Ktor WebSocket 사용하기

|

Ktor WebSocket 사용하기

WebSocket를 이용하면 실시간 양방향 통신을 할 수 있습니다.


build.gradle

먼저 build.gradle에 다음 라이브러리를 추가해줍니다.

dependencies {
    implementation "io.ktor:ktor-websockets:$ktor_version"
}


Main.kt

routing은 다음 코드처럼 할 수 있으며, incoming.receive() 메소드와 outgoing.send(Frame.Text(text) 메소드는 블럭킹(Blocking) 메소드입니다.

package com.snowdeer

import io.ktor.application.Application
import io.ktor.application.install
import io.ktor.http.cio.websocket.Frame
import io.ktor.http.cio.websocket.readText
import io.ktor.http.content.resources
import io.ktor.http.content.static
import io.ktor.routing.routing
import io.ktor.websocket.WebSockets
import io.ktor.websocket.webSocket
import java.time.Duration

fun Application.main() {

    install(WebSockets) {
        pingPeriod = Duration.ofSeconds(60) // Disabled (null) by default
        timeout = Duration.ofSeconds(15)
        maxFrameSize = Long.MAX_VALUE // Disabled (max value). The connection will be closed if surpassed this length.
        masking = false
    }

    routing {
        static("/static") {
            resources("static")
        }

        webSocket("/chat") {
            println("[snowdeer] chat starts")
            while (true) {
                when (val frame = incoming.receive()) {
                    is Frame.Text -> {
                        val text = frame.readText()
                        println("[snowdeer] text: $text")
                        outgoing.send(Frame.Text("$text from Server"))
                    }
                }
            }

            println("[snowdeer] chat is finished")
        }
    }
}


client.html

다음 코드를 브라우저에서 실행해서 WebSocket 통신이 잘 되는지 확인할 수 있습니다.

<!DOCTYPE HTML>

<html>
   <head>
      
      <script type = "text/javascript">
         function startWebSocket() {
            
            if ("WebSocket" in window) {
               alert("You can use WebSocket.");
               
               var ws = new WebSocket("ws://localhost:8080/chat");
				
               ws.onopen = function() {
                  alert("WebSocket is opened.");
                  ws.send("Hello");
                  alert("Send message to Server(Hello).");
               };
				
               ws.onmessage = function (evt) { 
                  var msg = evt.data;
                  alert("Message is received(" + msg + ")");
               };
				
               ws.onclose = function() { 
                  alert("WebSocket is closed."); 
               };
            } else {
               alert("Your browser does not support WebSocket !!!");
            }
         }
      </script>
		
   </head>
   
   <body>
      <a href = "javascript:startWebSocket()">Start WebSocket</a>
   </body>
</html>