|  |  |  |  | package controller | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | import ( | 
					
						
							|  |  |  |  | 	"encoding/json" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	"flx/mister" | 
					
						
							|  |  |  |  | 	"flx/websocketservers" | 
					
						
							|  |  |  |  | 	"net/http" | 
					
						
							|  |  |  |  | 	"time" | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							|  |  |  |  | 	"github.com/gorilla/websocket" | 
					
						
							|  |  |  |  | 	log "github.com/sirupsen/logrus" | 
					
						
							|  |  |  |  | ) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // WsPage is a websocketservers handler
 | 
					
						
							|  |  |  |  | func WsPage(c *gin.Context) { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	defer func() { | 
					
						
							|  |  |  |  | 		if r := recover(); r != nil { | 
					
						
							|  |  |  |  | 			log.Error("WsPage:", r) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	}() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// change the reqest to websocketservers model
 | 
					
						
							|  |  |  |  | 	//将http协议升级成websocket协议
 | 
					
						
							|  |  |  |  | 	conn, error := (&websocket.Upgrader{ | 
					
						
							|  |  |  |  | 		//
 | 
					
						
							|  |  |  |  | 		ReadBufferSize: 1023, | 
					
						
							|  |  |  |  | 		//
 | 
					
						
							|  |  |  |  | 		WriteBufferSize: 1023, | 
					
						
							|  |  |  |  | 		//允许跨域
 | 
					
						
							|  |  |  |  | 		CheckOrigin: func(r *http.Request) bool { | 
					
						
							|  |  |  |  | 			return true | 
					
						
							|  |  |  |  | 		}, | 
					
						
							|  |  |  |  | 	}).Upgrade(c.Writer, c.Request, nil) | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if error != nil { | 
					
						
							|  |  |  |  | 		http.NotFound(c.Writer, c.Request) | 
					
						
							|  |  |  |  | 		return | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	//可以用传来的参数识别身份
 | 
					
						
							|  |  |  |  | 	roomid := c.Query("roomid") | 
					
						
							|  |  |  |  | 	uid := c.Query("userid") //staffid
 | 
					
						
							|  |  |  |  | 	log.Println(roomid, "------roomid-----uid----", uid) | 
					
						
							|  |  |  |  | 	// websocketservers connect
 | 
					
						
							|  |  |  |  | 	//这里是随机生成id
 | 
					
						
							|  |  |  |  | 	//每一次连接都会新开一个client,client.id通过uuid生成保证每次都是不同的
 | 
					
						
							|  |  |  |  | 	//client := &websocketservers.Client{Id: uuid.NewV4().String(), Socket: conn, Send: make(chan []byte)}
 | 
					
						
							|  |  |  |  | 	client := &websocketservers.Client{Id: uid, Socket: conn, Send: make(chan []byte)} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	//注册一个新的链接
 | 
					
						
							|  |  |  |  | 	websocketservers.Manager.Register <- client | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	//启动协程收web端传过来的消息
 | 
					
						
							|  |  |  |  | 	go client.Read() | 
					
						
							|  |  |  |  | 	//启动协程把消息返回给web端
 | 
					
						
							|  |  |  |  | 	go client.Write() | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // 测试 websocket 连通性
 | 
					
						
							|  |  |  |  | func SendMessageByUid(c *gin.Context) { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	defer func() { | 
					
						
							|  |  |  |  | 		if r := recover(); r != nil { | 
					
						
							|  |  |  |  | 			log.Error("SendMessageByUid:", r) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	}() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	// 先声明map
 | 
					
						
							|  |  |  |  | 	var parametermap map[string]interface{} | 
					
						
							|  |  |  |  | 	// 再使用make函数创建一个非nil的map,nil map不能赋值
 | 
					
						
							|  |  |  |  | 	parametermap = make(map[string]interface{}) | 
					
						
							|  |  |  |  | 	err := DecodeJsonPayload(c, ¶metermap) | 
					
						
							|  |  |  |  | 	if err != nil { | 
					
						
							|  |  |  |  | 		log.Println(err) | 
					
						
							|  |  |  |  | 		return | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	if parametermap["userid"] != nil && parametermap["message"] != nil { | 
					
						
							|  |  |  |  | 		jsonMessage, _ := json.Marshal(&websocketservers.Message{ | 
					
						
							|  |  |  |  | 			ContentType: "ssjk", | 
					
						
							|  |  |  |  | 			Sender:      parametermap["userid"].(string), | 
					
						
							|  |  |  |  | 			Content:     parametermap["message"].(string), | 
					
						
							|  |  |  |  | 			ContentTime: time.Now().Format("2006-01-02 15:04:05")}) | 
					
						
							|  |  |  |  | 		// log.Println(string(jsonMessage))
 | 
					
						
							|  |  |  |  | 		websocketservers.Manager.SendMessageByClientId(jsonMessage, parametermap["userid"].(string)) | 
					
						
							|  |  |  |  | 	} | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	return | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | func WsIndexs(c *gin.Context) { | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	defer func() { | 
					
						
							|  |  |  |  | 		if r := recover(); r != nil { | 
					
						
							|  |  |  |  | 			log.Error("WsIndexs:", r) | 
					
						
							|  |  |  |  | 		} | 
					
						
							|  |  |  |  | 	}() | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | 	c.Header("content-type", "text/html;charset=utf-8") | 
					
						
							|  |  |  |  | 	c.String(http.StatusOK, string(mister.MeetingviewHtml)) | 
					
						
							|  |  |  |  | 	return | 
					
						
							|  |  |  |  | } |