본문 바로가기
WAS

WebLogic 12c WebSocket 구현

by 수앙 2020. 4. 30.

WebLogic 12c(12.2.1.4) 기준으로

웹로직 기반 웹소켓을 구현

 

1. 웹로직용 웹소켓이 들어있는 jar

<경로>/oracle/wls12214/wlserver/server/lib/api.jar

 

2. 서버 웹소켓 클래스 작성

import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;

@ServerEndpoint("/ws/echo")
public class WsEchoEndPoint {

	/**
	 * 클라이언트가 연결 시도했을 시
	 * 
	 * @param session
	 * @throws IOException
	 */
	@OnOpen
	public void onOpen(Session session) {
		System.out.println("Session on open!");
	}

	/**
	 * 클라리언트로부터 받은 메시지
	 * 
	 * @param message
	 * @return
	 */
	@OnMessage
	public String onMessage(String message) {
		System.out.println("Session receive message: " + message);
		return "ping".equals(message) ? "pong" : message;
	}

	/**
	 * 에러
	 * 
	 * @param t
	 */
	@OnError
	public void onError(Session session, Throwable t) {
		t.printStackTrace();
	}

	/**
	 * 세션 닫힘
	 * 
	 * @param session
	 */
	@OnClose
	public void onClose(Session session) {
		System.out.println("Session on close!");
	}

}

 

3. 클라이언트 웹소켓 자바스크립트 작성

<!doctype html>
<html lang="ko">
<head>
<meta charset="utf-8">
<title>WebSocket Test</title>
</head>
<body>

<div id="console"></div>

<script type="text/javascript">
	var ws;
	
	function goOpen() {
		if (ws != null && ws.readyState != WebSocket.CLOSED) {
			goClose();
		}
		
		ws = new WebSocket('ws://localhost:7001/ws/echo');
		ws.onopen = function() {
			goKeepAlive();
		    console.log('websocket opened.');
		};
		ws.onmessage = function(message) {
			if (message.data != 'pong') {
				$('#console').append(message.data);
			}
		};
		ws.onclose = function(event) {
		    console.log('websocket closed : ', event);
		};
	}
	
	function goKeepAlive() {
	    if (ws != null && ws.readyState == WebSocket.OPEN) {  
	        ws.send('ping');
	        console.log('websocket keepAlive.');
		    setTimeout(goKeepAlive, 5000);
	    }  
	}
	
	function goClose() {
		if (ws != null && ws.readyState != WebSocket.CLOSED) {
			ws.close();
			console.log('websocket closed.');
		}
	}
</script>
	
</body>
</html>

 

4. web.xml 수정 - async-supported 태그 추가

async-supported 태그가 없을 경우 웹소켓 수행 시 "The async-support is disabled on this request"와 같은 에러가 발생한다.

async-supported 태그 추가할 때 아래 예제와 같은 순서에 있어야 한다.

그렇지 않으면 WAS 구동 시 에러가 발생한다.

<!-- 필터가 있을 경우 -->
<filter>
	<filter-name>...</filter-name>
	<filter-class>...</filter-class>
	<async-supported>true</async-supported>
	<init-param>
		...
	</init-param>
</filter>

<!-- 서블릿이 있을 경우 -->
<servlet>
	<servlet-name>...</servlet-name>
	<servlet-class>...</servlet-class>
	<init-param>
		...
	</init-param>
	<load-on-startup>1</load-on-startup>
	<async-supported>true</async-supported>
</servlet>

 

참고사이트

https://docs.oracle.com/middleware/1221/wls/WLPRG/websockets.htm#WLPRG805

 

18 Using the WebSocket Protocol in WebLogic Server

In WebLogic Server, you deploy a WebSocket application as a Web application archive (WAR), either as a standalone Web application or a WAR module within an enterprise application. Therefore, many security practices that you apply to securing Web applicatio

docs.oracle.com

 

끝.

'WAS' 카테고리의 다른 글

context.xml JNDI 설정  (0) 2016.09.29
Tomcat HTTPS(SSL) 설정  (0) 2011.09.10

댓글