ÃֽŠ°Ô½Ã±Û(JAVA)
2019.01.13 / 22:15

À¥¼ÒÄÏ ¼­¹ö ¸¸µé±â

hanulbit
Ãßõ ¼ö 322

ȯ°æÀº,

Spring 4

Tomcat 7.0

JDK 1.7 


¾Æ·¡´Â WEBSocket Server ÀÌ°í 

JAVAX 7 api¿¡¼­ Á¦°øÇÏ´Â ¾î³ëÅ×À̼ÇÀ» »ç¿ëÇؼ­ ¼­¹ö¸¦ ±¸ÃàÇغ»´Ù.

¹°·Ð @ServerEndpoint ¶ó´Â ¾î³ëÅ×À̼ÇÀ» ÅëÇØ ÀÌ Å¬·¡½º´Â ¼­¹ö¶ó´Â °ÍÀ» ¸í½ÃÇÑ´Ù

½Ì±ÛÅæ ÆÐÅÏÀ¸·Î °ü¸®µÈ´Ù.


@ServerEndpoint¶õ

ÀÌ ¾î³ëÅ×À̼ÇÀ» ¸í½ÃÇÔÀ¸·Î¼­ WEB ¼ÒÄÏÀ¸·Î Á¢¼Ó °¡´ÉÇÑ URL Á¤º¸¸¦ ¸í½ÃÇÏ¿© ¼ÒÄÏ ¼­¹ö¸¦ »ý¼ºÇØÁÖ¸ç ÇÁ·ÎÆÛƼ¸¦ ÅëÇØ decoder³ª encoder¸¦ ¸í½ÃÇÒ ¼ö ÀÖ´Ù. 


JAVA

import javax.websocket.OnClose;

import javax.websocket.OnMessage;

import javax.websocket.OnOpen;

import javax.websocket.Session;

import javax.websocket.server.ServerEndpoint;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.ese.util.log.COMLog;


/**

 * WebSocket Server

 * @author GOEDOKID

 * @since 2015.03.17

 * @category class

 * @version 0.1

 */

@Component

@ServerEndpoint("/echo")

public class SocketServer {


static Set<Session> sessionUsers = Collections.synchronizedSet(new HashSet<Session>());

/**

 * When the client try to connection to websocket server,

 * open session and add information to the collection.

 *

 * @author GOEDOKID

 * @since 2015. 3. 18. 

 * @param Session userSession

 * @return

 */

@OnOpen

public void handleOpen(Session userSession) {

COMLog.info("WebSocket : Client Session is Open. ID is "+ userSession.getId());

sessionUsers.add(userSession);

}


/**

 * Send to message designated client by "websocket.send()" command.

 *

 * @author GOEDOKID

 * @since 2015. 3. 18. 

 * @param String message

 * @return

 */

@OnMessage

public void handleMessage(String message) throws IOException {

Iterator<Session> iterator = sessionUsers.iterator();

COMLog.info("WebSocket : Send message to all client.");

if(sessionUsers.size() > 0) {

while(iterator.hasNext()) {

iterator.next().getBasicRemote().

                   sendText(JSONConverter(message, "message", "event"));

}

} else {

COMLog.info("WebSocket : Here is no registered destination.");

}

}


/**

 * Session remove When browser down or close by client control

 * 

 * @author GOEDOKID

 * @since 2015. 3. 18. 

 * @param 

 * @return

 */

@OnClose

public void handleClose(Session session) {

COMLog.

info("WebSocket : Session remove complete. ID is "+session.getId());

sessionUsers.remove(session);

}

public String JSONConverter(String message, String command, String type) {

JSONObject jsonObject = new JSONObject();

jsonObject.put("type", type);

jsonObject.put("command", command);

jsonObject.put("message", message);

return jsonObject.toString();

}

}


°£´ÜÇÑ ±¸ÇöÀ» ÅëÇؼ­ Ŭ·¡½º Çϳª¸¦ WebSocket Client¸¦ ¹ÞÀ» ¼ö ÀÖ´Â ¼­¹ö·Î ±¸ÃàÇÒ ¼ö ÀÖ°í JAVAX 7 api¸¦ ÅëÇؼ­ ¾î³ëÅ×À̼ÇÀ» importÇÒ ¼ö ÀÖ´Ù. ¸¸¾à¿¡ °³¹ßȯ°æÀ̳ª pom.xml¿¡ javax 7¿¡ ´ëÇØ dependency°¡ ¸í½ÃµÇ¾î ÀÖÁö ¾Ê´Ù¸é ¿¡·¯°¡ ³¯ ¼ö ÀÖ´Ù.


JSP

var _ws = null;

_ws = window.WebSocket ? new WebSocket(url) : null;


_ws.onmessage = function(event){

msg = JSON.parse(event.data);

if(msg.command == "message"){

switch(msg.type){

case "event":{

makeSomeNoise(msg.message);

break;

}

}

}

};


ÃÖÃÊ À¥ ¼ÒÄÏÀÌ »ý¼ºµÇ´Â ½ÃÁ¡¿¡¼­ @OnOpen ¾î³ëÅ×À̼ÇÀÌ ¸í½ÃµÈ method¸¦ ÀÎÁöÇÏ¿© sessionÀ» openÇÏ°Ô µÈ´Ù. 

±×¸®°í 

@onMessage¸¦ ÅëÇؼ­ ¼Û½ÅµÈ µ¥ÀÌÅÍ´Â ¾Æ·¡ÀÇ _ws.onmessage¸¦ ÅëÇؼ­ ¼ö½ÅÇÒ ¼ö ÀÖ´Ù.


¿ø·¡ WebSocket ÀÌÀü¿¡´Â ½Ç½Ã°£ ¼­¹ö¿Í µ¥ÀÌÅ͸¦ ¼Û½ÅÇÏ´Â ºÎºÐ¿¡ ´ëÇؼ­ ·Õ Æú¸µÀ̳ª ÄÚ¸ä ¹æ½ÄÀ¸·Î µ¥ÀÌÅ͸¦ ½Ç½Ã°£À¸·Î °¡Á®¿À±â´Â ÇßÁö¸¸ ³»ºÎÀûÀ¸·Î ajax¸¦ ÅëÇؼ­ Ç×»ó ¼­¹ö¿¡ µ¥ÀÌÅÍ°¡ Á¸ÀçÇÏ´ÂÁö¿¡ ´ëÇؼ­ üũÇÒ ÇÊ¿ä°¡ ÀÖ¾ù´Ù. Ç×»ó ºê¶ó¿ìÀú´Â ¹«½¼ ÀÏÀ» ÇÏ°í ÀÖ¾ú°í ¸®¼Ò½º Á¡À¯¿¡ ´ëÇÑ À̽´°¡ ÀÖ¾ú´Ù. 


ÇÏÁö¸¸ WebSocketÀ» ÅëÇؼ­ ÆíÇÏ°Ô ½Ç½Ã°£ µ¥ÀÌÅ͸¦ ÁÖ°í ¹ÞÀ» ¼ö ÀÖ°Ô µÆ´Ù.


Âü°í.

1. ¾Ë°íº¸´Ï @ServerPoint µîµî ÀÌ·± ¾î³ëÅ×À̼ÇÀº tomcat 7.0.4 ÀÌ»ó ¹öÀü¿¡ Æ÷ÇÔÇÏ°í ÀÖ´Â ¶óÀ̺귯¸®¿¡¼­ Áö¿øÇÑ´Ù. ÀÌÀü ¹öÀüÀÏ¾Æ ¸í½ÃµÈ ¹öÀü À̻󿡼­ ¶óÀ̺귯¸®°¡ »óÀÌÇÏ°Ô Æ÷ÇÔÇÏ°í ÀÖ´Â °É È®ÀÎÇß´Ù.