大大大大涛 2017-11-16 02:44 采纳率: 0%
浏览 2697

Websocket通过注解连不上service

用了好的办法就是死活打印不出来service里面的值,使用别的controller可以打印出来service里面的东西
package com.xinhaiwang.controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

import javax.servlet.http.HttpServletRequest;
import javax.websocket.CloseReason;
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;

import org.springframework.beans.factory.annotation.Autowired;

import com.xinhaiwang.service.MklService;

@ServerEndpoint("/echo")
public class Websocket {
@Autowired
private MklService mklservice;
/*private MklService mklservice=(MklService) ContextLoader.getCurrentWebApplicationContext().getBean("mklservice");*/
//concurrent包的线程安全Set,用来存放每个客户端对应的MyWebSocket对象。若要实现服务端与单一客户端通信的话,可以使用Map来存放,其中Key可以为用户标识

private static CopyOnWriteArraySet webSocketSet = new CopyOnWriteArraySet();

//与某个客户端的连接会话,需要通过它来给客户端发送数据

private Session session;

@OnOpen
public void onOpen(Session session) throws IOException {
System.out.println("onOpen");
this.session = session;

webSocketSet.add(this); //加入set中

System.out.println("有新连接加入!");
//以下代码省略...
}
@OnMessage
public String onMessage(String message) throws Exception {
System.out.println("onMessage");
System.out.println("验证连通性:");
/* DmzController dmz = new DmzController();
System.out.println(dmz.mkl());
mklservice.getCount();*/
//向前端传送消息
return "77777";
//以下代码省略...
}
/*@OnMessage(maxMessageSize=6)
public void receiveMessage(String message) {
System.out.println("receiveMessage");
System.out.println("来自客户端的消息:" + message);

//以下代码省略...
}*/
@OnError
public void onError(Throwable t) {
System.err.println("onError");
//以下代码省略...
}

 @OnClose
 public void onClose(Session session, CloseReason reason) {
     System.err.println("onClose");

 //以下代码省略...
 } 
 public void sendMessage(String message) throws IOException{ 
        //调用写好的方法向前端发起请求并一块传值
        this.session.getBasicRemote().sendText(message);  
        //this.session.getAsyncRemote().sendText(message);  
    }

/**
 * 获取数组
 * @throws Exception 
 */

    public String getbean() throws Exception{
        System.out.println("进入到自定义方法");
        //判断时间
         Map<String, Object>map = new HashMap<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String now = df.format(new Date());

        String time = "2017-11-19 11:00:00";
        System.out.println("获取当前系统时间"+now);// new Date()为获取当前系统时间
        System.out.println("获取自定义时间"+time);//获取自定义时间
        /*System.out.println("总条数"+service.getCount());*/
        String sjh[] = new String[10];
        map.put("data", sjh);
        //判断是否到抽奖时间
        System.out.println("判断时间:"+!now.equals(time));
        mklservice.getCount();
        System.out.println("获取总条数"+mklservice.getCount());
        /*if(!now.equals(time)){
            //查到中奖的手机号做成数组(加个 判断数组.length是否等于十)
                for (int i = 0; i < 10; i++) {
                    //生成随机数作为id
                    boolean a = true;
                    Integer x=(int)(Math.random()*100);
                    while (a) {
                        System.out.println("随机数:"+x);
                        Integer id = mklservice.getCount();
                        System.out.println("总条数"+id);
                        if(id<x){
                            a = true;
                        }else{
                            a = false;
                            Mkl mkl = mklservice.getbean(x);
                            sjh[i]=mkl.getPhone();
                            mkl.setYuliu1("1");
                        }

                    }
                }
            map.put("data", sjh);
        }*/
            return "";
    }
}
  • 写回答

7条回答 默认 最新

  • 乐古 2017-11-16 03:11
    关注

    可能是你的MklService 没有加注解@Service

    评论

报告相同问题?

悬赏问题

  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)
  • ¥15 彩灯控制电路,会的加我QQ1482956179
  • ¥200 相机拍直接转存到电脑上 立拍立穿无线局域网传
  • ¥15 (关键词-电路设计)
  • ¥15 如何解决MIPS计算是否溢出
  • ¥15 vue中我代理了iframe,iframe却走的是路由,没有显示该显示的网站,这个该如何处理
  • ¥15 操作系统相关算法中while();的含义
  • ¥15 CNVcaller安装后无法找到文件
  • ¥15 visual studio2022中文乱码无法解决