當前位置: 首頁>>代碼示例>>Golang>>正文


Golang WSChannel.Refresh方法代碼示例

本文整理匯總了Golang中github.com/b3log/wide/util.WSChannel.Refresh方法的典型用法代碼示例。如果您正苦於以下問題:Golang WSChannel.Refresh方法的具體用法?Golang WSChannel.Refresh怎麽用?Golang WSChannel.Refresh使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/b3log/wide/util.WSChannel的用法示例。


在下文中一共展示了WSChannel.Refresh方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: WSHandler

// WSHandler handles request of creating Shell channel.
func WSHandler(w http.ResponseWriter, r *http.Request) {
	httpSession, _ := session.HTTPSession.Get(r, "wide-session")
	if httpSession.IsNew {
		http.Error(w, "Forbidden", http.StatusForbidden)

		return
	}
	username := httpSession.Values["username"].(string)

	sid := r.URL.Query()["sid"][0]

	conn, _ := websocket.Upgrade(w, r, nil, 1024, 1024)
	wsChan := util.WSChannel{Sid: sid, Conn: conn, Request: r, Time: time.Now()}

	ret := map[string]interface{}{"output": "Shell initialized", "cmd": "init-shell"}
	err := wsChan.WriteJSON(&ret)
	if nil != err {
		return
	}

	ShellWS[sid] = &wsChan

	logger.Debugf("Open a new [Shell] with session [%s], %d", sid, len(ShellWS))

	input := map[string]interface{}{}

	for {
		if err := wsChan.ReadJSON(&input); err != nil {
			logger.Error("Shell WS ERROR: " + err.Error())

			return
		}

		inputCmd := input["cmd"].(string)

		cmds := strings.Split(inputCmd, "|")
		commands := []*exec.Cmd{}
		for _, cmdWithArgs := range cmds {
			cmdWithArgs = strings.TrimSpace(cmdWithArgs)
			cmdWithArgs := strings.Split(cmdWithArgs, " ")
			args := []string{}
			if len(cmdWithArgs) > 1 {
				args = cmdWithArgs[1:]
			}

			cmd := exec.Command(cmdWithArgs[0], args...)
			commands = append(commands, cmd)
		}

		output := ""
		if !strings.Contains(inputCmd, "clear") {
			output = pipeCommands(username, commands...)
		}

		ret = map[string]interface{}{"output": output, "cmd": "shell-output"}

		if err := wsChan.WriteJSON(&ret); err != nil {
			logger.Error("Shell WS ERROR: " + err.Error())
			return
		}

		wsChan.Refresh()
	}
}
開發者ID:EricRobert,項目名稱:wide,代碼行數:65,代碼來源:shells.go


注:本文中的github.com/b3log/wide/util.WSChannel.Refresh方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。