Selaa lähdekoodia

feat;增加消息通知接口

v1.0.4
yuan_rh 4 vuotta sitten
vanhempi
commit
6ea48b2551
4 muutettua tiedostoa jossa 55 lisäystä ja 13 poistoa
  1. +2
    -0
      config/config.go
  2. +22
    -10
      handle/handle.go
  3. +1
    -1
      main.go
  4. +30
    -2
      websocket/websocket.go

+ 2
- 0
config/config.go Näytä tiedosto

@@ -24,6 +24,8 @@ var ServerIpfsUrl = "10.240.10.238:5001"
var UpdaterName ="LOCKING更新.exe"
var EtcdUrl="127.0.0.1:2379"

var ServerUrl = "http://www.lockingos.org:9003"

var GobalWatch *fsnotify.Watcher

//全局的chanel map


+ 22
- 10
handle/handle.go Näytä tiedosto

@@ -43,6 +43,9 @@ var gobalLocalProjectDir string

var gobalSubscriptionFileChangeSwitch int =0 //订阅文件变更开关

//全局消息通知管道
var GobalMessageNotify = make(chan string,1000)



var ipfsPath=os.Getenv("IPFS-PATH")
@@ -615,7 +618,7 @@ type FileParam struct {

//获取文件的历史版本管理文件最新hash值
func postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath string)(string,error){
url:="http://www.lockingos.org:9003/api/pms/sdk/queryFileHistoryCurrentCid"
url:=config.ServerUrl+"/api/pms/sdk/queryFileHistoryCurrentCid"
contentType := "application/json"

fileParam :=FileParam{
@@ -674,7 +677,7 @@ func postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath s
//更新文件记录
func postUpdateFile(projectName, folderName, relativePath, ipfsCid, fileName, historyCurrentIpfsCid, historyPreIpfsCid,userId string, fileSize int64, fileVersion int)(err error){

url:="http://www.lockingos.org:9003/api/pms/sdk/updateFile"
url:=config.ServerUrl+"/api/pms/sdk/updateFile"
contentType := "application/json"
intUserId,_:=strconv.ParseInt(userId,10,64)

@@ -1041,18 +1044,24 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
for actionAndModifyFilePathStr :=range config.GobalWatchChannelMap[gobalLocalProjectDir] {
//log.Println(actionAndModifyFilePathStr)
actionAndModifyFilePath := strings.Split(actionAndModifyFilePathStr,";")
queryKey := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1)

//当前登陆用户判断
if gobalLoginUserName != strings.Split(queryKey,"\\")[0]{
log.Printf("非法用户修改%v", actionAndModifyFilePathStr)
continue
}

if actionAndModifyFilePath[0]=="remove"{
k := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1)
queryMap,err :=etcdclient.QueryWithPrefix(k)
queryMap,err :=etcdclient.QueryWithPrefix(queryKey)
if len(queryMap)==0{
continue
}
err = etcdclient.DeleteWithPrefix(k)
err = etcdclient.DeleteWithPrefix(queryKey)
if err != nil {
log.Println(err)
}
}else if actionAndModifyFilePath[0]=="write"{
queryKey := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1)
querymap,err := etcdclient.QueryWithPrefix(queryKey)
if err != nil {
log.Println(err)
@@ -1072,9 +1081,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
log.Printf("文件变更 [ %v ] write", actionAndModifyFilePathStr)

//如果非新增文件则自动post
//if goabalAddFileMap[]
filePath := actionAndModifyFilePath[1]
//TODO

//获取文件的历史版本管理文件hash
fileName :=filepath.Base(filePath)
@@ -1088,6 +1095,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
}
historyCurrentHash,err :=postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath)
if err!=nil{
GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName)
log.Printf("postGetHistoryCurrentHash 返回失败,%v",err.Error())
continue
}
@@ -1095,6 +1103,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
//自动更新文件
err =UploadCommand(nil,filePath,fileName,projectName,dir,historyCurrentHash,"",gobalLoginUserName,false)
if err!=nil{
GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName)
log.Printf("UploadCommand 返回失败,%v",err.Error())
//记录修改状态
newValue := oldValue[0]+";" +"1"
@@ -1105,9 +1114,9 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
}
continue
}
GobalMessageNotify <- fmt.Sprintf("文件更新成功\n%v",fileName)

}else if actionAndModifyFilePath[0]=="create"{
queryKey := strings.Replace(actionAndModifyFilePath[1],config.LocalWorkSpaceDir,"",1)
querymap,err := etcdclient.QueryWithPrefix(queryKey)
if err != nil {
log.Println(err)
@@ -1129,7 +1138,6 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
//如果非新增文件则自动post
//if goabalAddFileMap[]
filePath := actionAndModifyFilePath[1]
//TODO

//获取文件的历史版本管理文件hash
fileName :=filepath.Base(filePath)
@@ -1143,6 +1151,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
}
historyCurrentHash,err :=postGetHistoryCurrentHash(projectName, folderName, fileName, relativePath)
if err!=nil{
GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName)
log.Printf("postGetHistoryCurrentHash 返回失败,%v",err.Error())
continue
}
@@ -1150,6 +1159,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
//自动更新文件
err =UploadCommand(nil,filePath,fileName,projectName,dir,historyCurrentHash,"",gobalLoginUserName,false)
if err!=nil{
GobalMessageNotify <- fmt.Sprintf("文件更新失败\n%v",fileName)
log.Printf("UploadCommand 返回失败,%v",err.Error())
//记录修改状态
newValue := oldValue[0]+";" +"1"
@@ -1160,6 +1170,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
}
continue
}
GobalMessageNotify <- fmt.Sprintf("文件更新成功\n%v",fileName)
}
err = sendFileListFromEtcd(keyPrefix,projectName,conn)
if err != nil {
@@ -1171,6 +1182,7 @@ func SubscriptionFileChange(conn *websocket.Conn, projectName string) error{
return nil
}


func sendFileListFromEtcd(keyPrefix,projectName string,conn *websocket.Conn) error{
dataMap,err := etcdclient.QueryWithPrefix(keyPrefix)
if err != nil {


+ 1
- 1
main.go Näytä tiedosto

@@ -180,7 +180,7 @@ func main() {
http.HandleFunc("/initClientConfig", websocket.InitClientConfigHandler)
//http.HandleFunc("/watchFile", websocket.WatchFileHandler)
http.HandleFunc("/keeplive", websocket.KeepliveHandler)
http.HandleFunc("/messageNotify", websocket.MessageNotifyHandler)
http.HandleFunc("/queryCommitHistory", websocket.QueryCommitHistoryHandler)
http.HandleFunc("/editCommitHistoryMilestone", websocket.EditCommitHistoryMilestoneHandler)



+ 30
- 2
websocket/websocket.go Näytä tiedosto

@@ -123,8 +123,8 @@ func InitLocalWorkSpaceHandler(w http.ResponseWriter, r *http.Request) {
//log.Println("param initLocalWorkSpaceHandler:"+string(data))

params :=strings.Split(string(data),"|")
//TODO userId
err := handle.InitLocalWorkSpace(conn,params[0],"367294106252087297",params[1])
//userId = "367294106252087297"
err := handle.InitLocalWorkSpace(conn,params[0],params[1],params[2])
if err!=nil{
log.Println(err)
goto ERR
@@ -423,6 +423,34 @@ ERR:

}

//消息通知
func MessageNotifyHandler(w http.ResponseWriter, r *http.Request){
//w.Write([]byte("hello"))
//收到http请求(upgrade),完成websocket协议转换
//在应答的header中放上upgrade:websoket
var (
conn *websocket.Conn
err error
//msgType int
//data []byte
)
if conn, err = upgrader.Upgrade(w, r, nil); err !=nil {
//报错了,直接返回底层的websocket链接就会终断掉
return
}

//读取通道消息
for msg := range handle.GobalMessageNotify {
if err := conn.WriteMessage(websocket.TextMessage, []byte(msg)); err != nil {
log.Println(err)
goto ERR
}
}
//error的标签
ERR:
conn.Close()
}

//查询历史版本记录
func QueryCommitHistoryHandler(w http.ResponseWriter, r *http.Request) {
//w.Write([]byte("hello"))


Ladataan…
Peruuta
Tallenna