parent
c0e0ad890e
commit
f7f1a40d56
@ -0,0 +1,15 @@
|
||||
package xyz.wbsite;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.scheduling.annotation.EnableScheduling;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableScheduling
|
||||
public class TestWebsocketApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(TestWebsocketApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package xyz.wbsite.config;
|
||||
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
|
||||
|
||||
@Configuration
|
||||
public class WebSocketConfig {
|
||||
@Bean
|
||||
public ServerEndpointExporter serverEndpointExporter() {
|
||||
return new ServerEndpointExporter();
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package xyz.wbsite.controller;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.ui.ModelMap;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@Controller
|
||||
public class IndexController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public String text(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
|
||||
map.put("name","张三");
|
||||
return "index";
|
||||
}
|
||||
}
|
@ -0,0 +1,55 @@
|
||||
package xyz.wbsite.test;
|
||||
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import okhttp3.WebSocket;
|
||||
import okhttp3.WebSocketListener;
|
||||
import okio.ByteString;
|
||||
|
||||
public class Test {
|
||||
public static void main(String[] args) {
|
||||
final WebSocket[] socket = new WebSocket[1];
|
||||
OkHttpClient mOkHttpClient = new OkHttpClient.Builder()
|
||||
.retryOnConnectionFailure(true)
|
||||
.build();
|
||||
|
||||
Request mRequest = new Request.Builder()
|
||||
.url("ws://127.0.0.1:8080/websocket/1")
|
||||
.build();
|
||||
|
||||
mOkHttpClient.newWebSocket(mRequest, new WebSocketListener() {
|
||||
@Override
|
||||
public void onOpen(WebSocket webSocket, Response response) {
|
||||
socket[0] = webSocket;
|
||||
System.out.println("连接已打开");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, String text) {
|
||||
System.out.println("接收消息:" + text);
|
||||
socket[0].send("收到消息");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, ByteString bytes) {
|
||||
super.onMessage(webSocket, bytes);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosing(WebSocket webSocket, int code, String reason) {
|
||||
System.out.println("连接关闭");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClosed(WebSocket webSocket, int code, String reason) {
|
||||
System.out.println("连接关闭");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
|
||||
System.out.println("连接失败");
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package xyz.wbsite.utils;
|
||||
|
||||
|
||||
public class StringUtils {
|
||||
|
||||
public static boolean isNull(String str) {
|
||||
return str == null;
|
||||
}
|
||||
|
||||
public static boolean isNotNull(String str) {
|
||||
return !isNull(str);
|
||||
}
|
||||
|
||||
public static boolean isBlank(String str) {
|
||||
|
||||
return str != null && "".equals(str);
|
||||
}
|
||||
|
||||
|
||||
public static boolean isNotBlank(String str) {
|
||||
return !isBlank(str);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
spring.freemarker.suffix=.ftl
|
||||
spring.freemarker.enabled=true
|
||||
spring.freemarker.allow-request-override=false
|
||||
spring.freemarker.cache=true
|
||||
spring.freemarker.check-template-location=true
|
||||
spring.freemarker.charset=UTF-8
|
||||
spring.freemarker.content-type=text/html
|
||||
spring.freemarker.expose-request-attributes=false
|
||||
spring.freemarker.expose-session-attributes=false
|
||||
spring.freemarker.expose-spring-macro-helpers=false
|
||||
spring.freemarker.settings.template_update_delay=1
|
||||
spring.freemarker.settings.locale=zh_CN
|
||||
spring.freemarker.settings.datetime_format=yyyy-MM-dd HH:mm:ss
|
||||
spring.freemarker.settings.date_format=yyyy-MM-dd
|
||||
spring.freemarker.settings.number_format=#.##
|
||||
spring.freemarker.settings.classic_compatible=true
|
||||
spring.freemarker.settings.whitespace_stripping=true
|
||||
spring.freemarker.settings.url_escaping_charset=utf-8
|
@ -0,0 +1,42 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
|
||||
</head>
|
||||
<body>
|
||||
<button onclick="javascript:WebSocketTest()">连接WebSocket</button>
|
||||
<div id="info"></div>
|
||||
<script type="text/javascript">
|
||||
function log(msg) {
|
||||
var li = document.createElement("li");
|
||||
li.innerText = msg;
|
||||
document.body.querySelector("#info").appendChild(li);
|
||||
}
|
||||
|
||||
function WebSocketTest() {
|
||||
if ("WebSocket" in window) {
|
||||
// 打开一个 web socket
|
||||
var ws = new WebSocket("ws://localhost:8080/websocket/1");
|
||||
|
||||
ws.onopen = function () {
|
||||
// Web Socket 已连接上,使用 send() 方法发送数据
|
||||
ws.send("发送数据");
|
||||
};
|
||||
|
||||
ws.onmessage = function (evt) {
|
||||
var received_msg = evt.data;
|
||||
log(received_msg)
|
||||
};
|
||||
|
||||
ws.onclose = function () {
|
||||
// 关闭 websocket
|
||||
log("连接已关闭...");
|
||||
};
|
||||
} else {
|
||||
// 浏览器不支持 WebSocket
|
||||
log("您的浏览器不支持 WebSocket!");
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in new issue