package com.dxhy.extend.websocket;

import java.util.Map;

import javax.annotation.Resource;
import javax.websocket.Session;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.controller.AbstractController;

/**
 * @author dxhy
 */
@RestController
@RequestMapping("websocketFegin")
public class ExtWebsocketFegin extends AbstractController {

    @Resource
    private ExtWebSocket websocket;

    @RequestMapping(value = "/setMessageToWebsocket", method = RequestMethod.POST)
    public void setMessageToWebsocket(@RequestBody String userName) {
        if (!StringUtils.isBlank(userName)) {
            Map<String, Session> map = ExtWebSocket.WEBSOCKET_USERINFO;
            Session session = map.get(userName);
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("userName", userName);
            websocket.onMessage(session, jsonObj.toJSONString());
        }
    }

}
