java spring 웹소켓
페이지 정보
본문
1. Spring 4.x, WebSocket
# pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
# servlet-context.xml
<websocket:handlers>
<websocket:mapping handler="webSocketHandler" path="/websocket" />
</websocket:handlers>
# WebSocketHandler.java
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class WebSocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
for (int i = 0; i < 10; i++) {
session.sendMessage(new TextMessage(message.getPayload()));
Thread.sleep(1000);
}
}
}
# websocket.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/resources/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var ws = new WebSocket('ws://localhost:8080/websocket');
ws.onopen = function() {
$('#console').append('websocket opened' + '<br>');
};
ws.onmessage = function(message) {
$('#console').append('receive message : ' + message.data + '<br>');
};
ws.onclose = function(event) {
$('#console').append('websocket closed : ' + event);
};
function messageSend() {
ws.send($('#message').val());
}
</script>
</head>
<body>
<input type="text" id="message" />
<input type="button" value="전송" onclick="messageSend();" />
<div id="console" />
</body>
</html>
2. Spring 4.x, SockJS
# 다운로드
- SockJS 다운로드 : https://github.com/sockjs/sockjs-client/tree/master/dist
# pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASEversion>
</dependency>
# servlet-context.xml
<websocket:handlers>
<websocket:mapping handler="webSocketHandler" path="/websocket" />
<websocket:sockjs />
</websocket:handlers>
# WebSocketHandler.java
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class WebSocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
for (int i = 0; i < 10; i++) {
session.sendMessage(new TextMessage(message.getPayload()));
Thread.sleep(1000);
}
}
}
# websocket.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/resources/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/resources/js/sockjs/sockjs-1.0.3.min.js"></script>
<script type="text/javascript">
var sock = new SockJS('http://localhost:8080/websocket');
sock.onopen = function() {
$('#console').append('websocket opened' + '<br>');
};
sock.onmessage = function(message) {
$('#console').append('receive message : ' + message.data + '<br>');
};
sock.onclose = function(event) {
$('#console').append('websocket closed : ' + event);
};
function messageSend() {
sock.send($('#message').val());
}
</script>
</head>
<body>
<input type="text" id="message" />
<input type="button" value="전송" onclick="messageSend();" />
<div id="console" />
</body>
</html>
3. Srping 4.x, SockJS, STOMP
# 다운로드
- SockJS 다운로드 : https://github.com/sockjs/sockjs-client/tree/master/dist
- STOMP 다운로드 : http://jmesnil.net/stomp-websocket/doc/
# pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASEversion>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
# servlet-context.xml
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/stomp">
<websocket:sockjs />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic" />
</websocket:message-broker>
# WebSocketController.java
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WebSocketController {
@MessageMapping("/stomp")
@SendTo("/topic/stomp")
public ResponseEntity<String> stomp(String request) {
return new ResponseEntity<String>(request, HttpStatus.OK);
}
}
# websocket.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/resources/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/resources/js/sockjs/sockjs-1.0.3.min.js"></script>
<script type="text/javascript" src="/resources/js/stomp/stomp.min.js"></script>
<script type="text/javascript">
var socket = new SockJS('/stomp');
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
$('#console').append('Connected: ' + frame + '<br>');
stompClient.subscribe('/topic/stomp', function(message) {
$('#console').append(message.body + '<br>');
});
});
function disconnect() {
if (stompClient != null) {
stompClient.disconnect();
}
$('#console').append('Disconnected<br>');
}
function messageSend() {
stompClient.send("/app/stomp", {}, $('#message').val());
}
</script>
</head>
<body>
<input type="text" id="message" />
<input type="button" value="전송" onclick="messageSend();" />
<input type="button" value="종료" onclick="disconnect();" />
<div id="console" />
</body>
</html>
출처: http://sooin01.tistory.com/entry/Spring-WebSocket [수앙]
# pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
# servlet-context.xml
<websocket:handlers>
<websocket:mapping handler="webSocketHandler" path="/websocket" />
</websocket:handlers>
# WebSocketHandler.java
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class WebSocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
for (int i = 0; i < 10; i++) {
session.sendMessage(new TextMessage(message.getPayload()));
Thread.sleep(1000);
}
}
}
# websocket.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/resources/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript">
var ws = new WebSocket('ws://localhost:8080/websocket');
ws.onopen = function() {
$('#console').append('websocket opened' + '<br>');
};
ws.onmessage = function(message) {
$('#console').append('receive message : ' + message.data + '<br>');
};
ws.onclose = function(event) {
$('#console').append('websocket closed : ' + event);
};
function messageSend() {
ws.send($('#message').val());
}
</script>
</head>
<body>
<input type="text" id="message" />
<input type="button" value="전송" onclick="messageSend();" />
<div id="console" />
</body>
</html>
2. Spring 4.x, SockJS
# 다운로드
- SockJS 다운로드 : https://github.com/sockjs/sockjs-client/tree/master/dist
# pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASEversion>
</dependency>
# servlet-context.xml
<websocket:handlers>
<websocket:mapping handler="webSocketHandler" path="/websocket" />
<websocket:sockjs />
</websocket:handlers>
# WebSocketHandler.java
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;
public class WebSocketHandler extends TextWebSocketHandler {
@Override
protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
for (int i = 0; i < 10; i++) {
session.sendMessage(new TextMessage(message.getPayload()));
Thread.sleep(1000);
}
}
}
# websocket.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/resources/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/resources/js/sockjs/sockjs-1.0.3.min.js"></script>
<script type="text/javascript">
var sock = new SockJS('http://localhost:8080/websocket');
sock.onopen = function() {
$('#console').append('websocket opened' + '<br>');
};
sock.onmessage = function(message) {
$('#console').append('receive message : ' + message.data + '<br>');
};
sock.onclose = function(event) {
$('#console').append('websocket closed : ' + event);
};
function messageSend() {
sock.send($('#message').val());
}
</script>
</head>
<body>
<input type="text" id="message" />
<input type="button" value="전송" onclick="messageSend();" />
<div id="console" />
</body>
</html>
3. Srping 4.x, SockJS, STOMP
# 다운로드
- SockJS 다운로드 : https://github.com/sockjs/sockjs-client/tree/master/dist
- STOMP 다운로드 : http://jmesnil.net/stomp-websocket/doc/
# pom.xml
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>4.2.1.RELEASEversion>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-messaging</artifactId>
<version>4.2.1.RELEASE</version>
</dependency>
# servlet-context.xml
<websocket:message-broker application-destination-prefix="/app">
<websocket:stomp-endpoint path="/stomp">
<websocket:sockjs />
</websocket:stomp-endpoint>
<websocket:simple-broker prefix="/topic" />
</websocket:message-broker>
# WebSocketController.java
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class WebSocketController {
@MessageMapping("/stomp")
@SendTo("/topic/stomp")
public ResponseEntity<String> stomp(String request) {
return new ResponseEntity<String>(request, HttpStatus.OK);
}
}
# websocket.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="/resources/js/jquery/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="/resources/js/sockjs/sockjs-1.0.3.min.js"></script>
<script type="text/javascript" src="/resources/js/stomp/stomp.min.js"></script>
<script type="text/javascript">
var socket = new SockJS('/stomp');
var stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
$('#console').append('Connected: ' + frame + '<br>');
stompClient.subscribe('/topic/stomp', function(message) {
$('#console').append(message.body + '<br>');
});
});
function disconnect() {
if (stompClient != null) {
stompClient.disconnect();
}
$('#console').append('Disconnected<br>');
}
function messageSend() {
stompClient.send("/app/stomp", {}, $('#message').val());
}
</script>
</head>
<body>
<input type="text" id="message" />
<input type="button" value="전송" onclick="messageSend();" />
<input type="button" value="종료" onclick="disconnect();" />
<div id="console" />
</body>
</html>
출처: http://sooin01.tistory.com/entry/Spring-WebSocket [수앙]
관련링크
- 이전글Centos 6.5 , install Predis and log4php for php 20.02.21
- 다음글프로그래밍 독학할때 자주 하는 실수 5가지 20.02.21
댓글목록
등록된 댓글이 없습니다.