1. nginx单纯做域名的代理时配置的解决办法
2. websocket中java类无法依赖注入的解决办法
3. nginx 域名映射对websocket中url的配置影响问题的解决办法
nginx配置
http{
include mime.types;
default_type application/octet-stream;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream web_pools {
server 127.0.0.1:8888;
}
server {
listen 80;
server_name ihg-tools.bds-analytics.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.3.52:8888;
}
location /websocket {
proxy_pass http://192.168.3.52:8888;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Origin "";
}
}
}
js代码
var socket;
$(function () {
if(typeof(WebSocket) == "undefined") {
layerMsg("错误","您的浏览器不支持WebSocket");
}else{
socket = new WebSocket("ws://ihg-tools.bds-analytics.com/websocket");
socket.onopen = function() {
send2service();
window.setInterval("send2service()",1000*120);
};
socket.onmessage = function(msg) {
layerMsg("警告",msg.data)
};
socket.onclose = function() {
console.log("Socket已关闭");
};
socket.onerror = function() {
layerMsg("错误","Socket发生了错误,此时可以尝试刷新页面");
}
}
});
function send2service() {
socket.send("hi,i am jack!");
}
java代码
package com.bds.ihgfb.util;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.bds.ihgfb.model.StatusBean;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.entity.Entity;
import org.nutz.dao.impl.NutDao;
import org.nutz.dao.impl.SimpleDataSource;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
@ServerEndpoint("/websocket")
@Component
public class WebSocketServer {
static Log log= LogFactory.get(WebSocketServer.class);
private static NutDao dao;
@Autowired
@Qualifier("xiaohongshu")
public void setDao(NutDao dao){
WebSocketServer.dao = dao;
}
private static int onlineCount = 0;
private static CopyOnWriteArraySet<WebSocketServer> webSocketSet = new CopyOnWriteArraySet<WebSocketServer>();
private Session session;
@OnOpen
public void onOpen(Session session) {
this.session = session;
webSocketSet.add(this);
addOnlineCount();
log.info("有新窗口开始监听当前在线人数为" + getOnlineCount());
}
@OnClose
public void onClose() {
webSocketSet.remove(this);
subOnlineCount();
log.info("有一连接关闭!当前在线人数为" + getOnlineCount());
}
@OnMessage
public void onMessage(String message, Session session) {
log.info("收到来自窗口的信息:"+session+"-----"+message);
String s = checkXhsFail();
for (WebSocketServer item : webSocketSet) {
try {
if ("fail".equals(s)){
sendMessage("请打开手机,检查是否存在滑块");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@OnError
public void onError(Session session, Throwable error) {
log.error("发生错误");
error.printStackTrace();
}
public void sendMessage(String message) throws IOException {
this.session.getBasicRemote().sendText(message);
}
public static synchronized int getOnlineCount() {
return onlineCount;
}
public static synchronized void addOnlineCount() {
WebSocketServer.onlineCount++;
}
public static synchronized void subOnlineCount() {
WebSocketServer.onlineCount--;
}
public String checkXhsFail(){
System.out.println("*********************定时任务开始执行*******************");
Sql sql = Sqls.create("select count(0) as count,status from t_note_simple_task group by status");
sql.setCallback(Sqls.callback.entities());
Entity<StatusBean> entity = dao.getEntity(StatusBean.class);
sql.setEntity(entity);
dao.execute(sql);
List<StatusBean> list = sql.getList(StatusBean.class);
int count = 0;
for (int i =0;i<list.size();i++){
StatusBean statusBean = list.get(i);
int status = statusBean.getStatus();
if (status==4){
count = statusBean.getCount();
}
}
if (count>=1){
System.out.println("》》推送《《");
return "fail";
}else {
return "suc";
}
}
}