Commit 6fb1d4d3 authored by 刘文胜's avatar 刘文胜

111111

parent f00d6914
......@@ -38,6 +38,7 @@ import org.springframework.web.socket.server.standard.ServerEndpointExporter;
import redis.clients.jedis.JedisPoolConfig;
import com.hdp.customerservice.websocket.*;
import com.hdp.pi.wechat.Subscriber;
@PropertySource(value="classpath:application.development.properties")
@ComponentScan
......@@ -72,7 +73,7 @@ public class APP extends WebMvcAutoConfiguration
}
@Bean
public IMWebSocketEndpoint reverseWebSocketEndpoint() {
public IMWebSocketEndpoint imWebSocketEndpoint() {
return new IMWebSocketEndpoint();
}
......
/**
* @Title: CustomerManager.java
* @Package com.hdp.customerservice.websocket
* @Description: TODO
* @author new12304508_163_com
* @date 2015年6月16日 下午4:49:51
* @version V1.0
*/
package com.hdp.customerservice.websocket;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.util.StringUtils;
import com.hdp.customerservice.websocket.model.Customer;
import com.hdp.pi.wechat.model.BaseEntity;
public class CustomerManager {
private static ConcurrentHashMap<String,Customer>
users = new ConcurrentHashMap<String, Customer>();
private CustomerManager(){}
public static Customer getUserByOpenId(String openId){
if(StringUtils.isEmpty(openId)){
return null;
}
return users.get(openId);
}
public static void addUser(Customer user){
if(user == null || StringUtils.isEmpty(user.openId)){
return;
}
users.put(user.openId, user);
}
public static void addUserIfNotExist(BaseEntity e){
String openId=e.openId;
Customer user = users.get(openId);
if(user == null){
user = new Customer();
user.openId= e.openId;
users.put(openId, user);
}
}
public static Customer[] getUsers(){
Customer[] us = new Customer[users.size()];
return users.values().toArray(us);
}
}
......@@ -11,28 +11,27 @@ package com.hdp.customerservice.websocket;
import java.io.IOException;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.LinkedBlockingQueue;
import javax.websocket.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hdp.customerservice.websocket.model.Customer;
import com.hdp.customerservice.websocket.model.Waiter;
import com.hdp.pi.wechat.model.BaseEntity;
@Component
public class IMManager {
private static Log log=LogFactory.getLog(IMManager.class);
public static ConcurrentHashMap<String,Customer>
users = new ConcurrentHashMap<String, Customer>();
public static ConcurrentHashMap<String,Waiter>
waiters = new ConcurrentHashMap<String, Waiter>();
private static LinkedBlockingQueue<BaseEntity> queue=new LinkedBlockingQueue<BaseEntity>();
public static void broadcast(String id,String message){
Iterator<Waiter> ws= waiters.values().iterator();
while(ws.hasNext()){
......@@ -47,23 +46,90 @@ public class IMManager {
}
}
public static void pushMessage(BaseEntity message) throws InterruptedException{
String openId=message.openId;
Customer user = users.get(openId);
if(user == null){
user = new Customer();
user.openId= message.openId;
user.waiter = waiters.get("lws");
users.put(openId, user);
public static void pushMessage(BaseEntity message){
CustomerManager.addUserIfNotExist(message);
try {
queue.put(message);
} catch (InterruptedException e) {
}
user.messages.put(message);
BaseEntity m = user.messages.poll();
if(user.waiter != null && user.waiter.session!=null && user.waiter.session.isOpen()){
try {
user.waiter.session.getBasicRemote().sendText(JSON.toJSONString(m));
} catch (IOException e) {
e.printStackTrace();
}
static{//测试用0.0
new Thread(new Runnable() {//消息发射器
public void hand(){
BaseEntity e=queue.poll();
if(e == null){
return ;
}
Customer customer=CustomerManager.getUserByOpenId(e.openId);
if(customer == null){
pushMessage(e);
return;
}
Waiter waiter = WaiterManager.getWaiterById(customer.waiterId);
if(waiter == null){
pushMessage(e);
return;
}
Session session= waiter.session;
if(session == null || !session.isOpen()){
pushMessage(e);
return;
}
try {
session.getBasicRemote().sendText(JSONObject.toJSONString(e));
} catch (IOException e1) {
pushMessage(e);
return;
}
}
}
@Override
public void run() {
while(true){
hand();
try {
Thread.sleep(10);
} catch (InterruptedException e) {
}
}
}
}).start();
new Thread(new Runnable() {//分配客服
public void hand(){
Customer[] cs = CustomerManager.getUsers();
Waiter[] ws=WaiterManager.getWaits();
for (Customer c : cs) {
if(!StringUtils.isEmpty(c.waiterId)){
continue;
}
for(Waiter w : ws){
if(w.customs.size() == 0){
c.waiterId=w.id;
w.customs.add(c.openId);
continue;
}
}
}
}
@Override
public void run() {
while(true){
hand();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}
}).start();
}
}
......@@ -23,27 +23,15 @@ public class IMWebSocketEndpoint {
@OnOpen
public void handleConnect(Session session) throws IOException{
Waiter waiter = new Waiter();
waiter.id=session.getId();
waiter.name="刘文胜"+Math.random();
waiter.session = session;
Iterator<Customer> cs= IMManager.users.values().iterator();
String openId= "";
while(cs.hasNext()){
Customer c=cs.next();
if(c.waiter == null || !c.waiter.session.isOpen()){
openId = c.openId;
break;
}
}
Customer user=IMManager.users.get(openId);
if(user!=null){
user.waiter=waiter;
}
IMManager.waiters.put("lws", waiter);
WaiterManager.addWaiter(waiter);
}
@OnClose
public void handleClose(Session session){
log.info("关闭");
log.info("111111111111111111111111111111111111111");
}
@OnMessage
......
/**
* @Title: WaiterManager.java
* @Package com.hdp.customerservice.websocket
* @Description: TODO
* @author new12304508_163_com
* @date 2015年6月16日 下午5:15:41
* @version V1.0
*/
package com.hdp.customerservice.websocket;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.util.StringUtils;
import com.hdp.customerservice.websocket.model.Customer;
import com.hdp.customerservice.websocket.model.Waiter;
public class WaiterManager {
private static ConcurrentHashMap<String,Waiter>
waiters = new ConcurrentHashMap<String, Waiter>();
private WaiterManager(){}
public static Waiter getWaiterById(String id){
if(StringUtils.isEmpty(id)){
return null;
}
return waiters.get(id);
}
public static void addWaiter(Waiter waiter){
if(waiter == null || StringUtils.isEmpty(waiter.id)){
return;
}
waiters.put(waiter.id, waiter);
}
public static Waiter[] getWaits(){
Waiter[] ws = new Waiter[waiters.size()];
return waiters.values().toArray(ws);
}
}
......@@ -17,14 +17,7 @@ public class Customer {
public String openId;
public String nickName;
public String headImg;
public Waiter waiter;
public String waiterId;
public Date lastTime;//最后活动时间
public LinkedBlockingQueue<BaseEntity> messages=new LinkedBlockingQueue<BaseEntity>();
}
\ No newline at end of file
......@@ -8,6 +8,9 @@
*/
package com.hdp.customerservice.websocket.model;
import java.util.ArrayList;
import java.util.List;
import javax.websocket.Session;
public class Waiter {
......@@ -18,4 +21,6 @@ public class Waiter {
public String headImg;
public Session session;
public List<String> customs= new ArrayList<String>();
}
package com.hdp.customerservice;
package com.hdp.pi.wechat;
import org.dom4j.Document;
import org.dom4j.DocumentException;
......
......@@ -150,7 +150,7 @@
<div ng-repeat="m in messages" ng-class="{true:'msg_bubble_list bubble_l',false:'msg_bubble_list bubble_r'}[m.type==1]" style="display: block">
<div class="bubble_mod clearfix">
<div class="bubble_user">
<a href="http://weibo.com/u/3955027752" target="_blank" class="face default_face"><img _src="#{avatarurl}" width="30" height="30" src="http://tp1.sinaimg.cn/3955027752/30/5728607898/1" /></a>
<a href="http://weibo.com/u/3955027752" target="_blank" class="face default_face"><img width="30" height="30" src="{{m.headimgurl}}" /></a>
</div>
<p class="bubble_name W_autocut" style="height: 15px;display:none" href="http://weibo.com/u/3955027752" target="_blank">海东青童鞋</p>
<p class="bubble_prompt" style="display: none;"><span class="W_icon icon_rederrorS"></span>发送失败,请输入验证码</p>
......@@ -164,8 +164,7 @@
</div>
<div class="bubble_main clearfix">
<div class="cont">
<p class="page">
{{m.message}}
<p class="page" ng-bind-html="m.message">
</p>
</div>
</div>
......
......@@ -82,26 +82,123 @@ websocketApp.controller('UserListCtrl',
}]);
websocketApp.controller('MessagesCtrl',
['$scope', '$rootScope', '$location', '$resource', '$stateParams','WebSocketService',
function($scope, $rootScope, $location, $resource, $stateParams,WebSocketService) {
['$scope', '$rootScope', '$location', '$resource', '$stateParams','$sce','WebSocketService',
function($scope, $rootScope, $location, $resource, $stateParams,$sce,WebSocketService) {
$scope.message = "";
var messages = $scope.messages = [];
var tplMessage={
touser:'ogoz1t65VSnKQQvYrD9-SLBL2KIk',
msgtype:'text',
text:
{
content:''
}
};
var news={
touser:'ogoz1t65VSnKQQvYrD9-SLBL2KIk',
msgtype:'news',
news:{
articles: [
{
title:'Happy Day',
description:'Is Really A Happy Day',
url:'https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a5c6f9f036fa828bd176cea39b227506/faf2b2119313b07ed3782dfd09d7912396dd8c79.jpg',
picurl:'https://ss1.baidu.com/9vo3dSag_xI4khGko9WTAnF6hhy/super/whfpf%3D425%2C260%2C50/sign=a5c6f9f036fa828bd176cea39b227506/faf2b2119313b07ed3782dfd09d7912396dd8c79.jpg'
}]
}
};
$scope.$on('text', function(event,data) {
$scope.$apply(function(){
console.log(data);
messages.push({type:1,message:data.content});
var m = {type:1,message:''};
m.message = $sce.trustAsHtml(data.content);
$resource('http://sandbox.wxpai.cn/open/user/info?appId=wx3e055e220ddde820&openId='+data.openId)
.get({},function(res){
m.headimgurl=res.data.headimgurl;
m.openid=res.data.openid;
},function(res){
});
messages.push(m);
});
});
$scope.$on('image', function(event,data) {
$scope.$apply(function(){
if(data.mediaId){
var m = {type:1,message:''};
var picUrl = 'http://sandbox.wxpai.cn/open/media/get?appId=wx3e055e220ddde820&mediaId='+data.mediaId;
m.message = $sce.trustAsHtml('<img src=\"'+picUrl+'" style="height:50px;width:50px;"/>');
messages.push(m);
}
});
});
function pv_q(u, w, h){
var pv='';
pv += '<object data="'+u+'" width="'+w+'" height="'+h+'" classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
pv += '<param name="src" value="'+u+'">';
pv += '<param name="controller" value="true">';
pv += '<param name="type" value="video/quicktime">';
pv += '<param name="autoplay" value="true">';
pv += '<param name="target" value="myself">';
pv += '<param name="bgcolor" value="black">';
pv += '<param name="pluginspage" value="http://www.apple.com/quicktime/download/index.html">';
pv += '<embed src="'+u+'" width="'+w+'" height="'+h+'" controller="true" align="middle" bgcolor="black" target="myself" type="video/quicktime" pluginspage="http://www.apple.com/quicktime/download/index.html"></embed>';
pv += '</object>';
return pv;
}
$scope.$on('voice', function(event,data) {
$scope.$apply(function(){
if(data.mediaId){
var m = {type:1,message:''};
var voiceUrl = 'http://sandbox.wxpai.cn/open/media/get?appId=wx3e055e220ddde820&mediaId='+data.mediaId;
/*m.message = $sce.trustAsHtml('<audio controls="controls" src=\"'+voiceUrl+'" style="height:50px;width:50px;"/>');*/
m.message = $sce.trustAsHtml(pv_q(voiceUrl,'150px','50px'));
messages.push(m);
}
});
});
$scope.$on('video', function(event,data) {
$scope.$apply(function(){
if(data.mediaId){
var m = {type:1,message:''};
var videoUrl = 'http://sandbox.wxpai.cn/open/media/get?appId=wx3e055e220ddde820&mediaId='+data.mediaId;
m.message = $sce.trustAsHtml('<video controls="controls" src=\"'+voiceUrl+'" width="100px;" heiht="100px"/>');
messages.push(m);
}
});
});
$scope.$on('shortvideo', function(event,data) {
$scope.$apply(function(){
if(data.mediaId){
var m = {type:1,message:''};
var shortvideoUrl = 'http://sandbox.wxpai.cn/open/media/get?appId=wx3e055e220ddde820&mediaId='+data.mediaId;
m.message = $sce.trustAsHtml('<video controls="controls" src=\"'+shortvideoUrl+'" width="100px;" heiht="100px"/>');
messages.push(m);
}
});
});
$scope.send= function(){
if($scope.message){
messages.push({type:2,message:$scope.message});
WebSocketService.sendMessage($scope.message).then(function(res) {
var m = {type:2,message:''};
m.message = $sce.trustAsHtml($scope.message);
messages.push(m);
/*WebSocketService.sendMessage($scope.message).then(function(res) {
messages.push({type:1,message:res.message});
}, function(res) {
console.log(res);
});
});*/
tplMessage.text.content=$scope.message;
$resource('http://sandbox.wxpai.cn/open/custom_send?appId=wx3e055e220ddde820')
.save(news,function(res){
console.log(res);
},function(res){
console.log(res);
});
$scope.message = "";
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment