当前位置: 首页>>代码示例>>Golang>>正文


Golang Session.Send方法代码示例

本文整理汇总了Golang中gopkg/in/igm/sockjs-go/v2/sockjs.Session.Send方法的典型用法代码示例。如果您正苦于以下问题:Golang Session.Send方法的具体用法?Golang Session.Send怎么用?Golang Session.Send使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在gopkg/in/igm/sockjs-go/v2/sockjs.Session的用法示例。


在下文中一共展示了Session.Send方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: echoHandler

func echoHandler(session sockjs.Session) {
	log.Println("new sockjs session established")
	var closedSession = make(chan struct{})
	chat.Publish("[info] new participant joined chat")
	defer chat.Publish("[info] participant left chat")
	go func() {
		reader, _ := chat.SubChannel(nil)
		for {
			select {
			case <-closedSession:
				return
			case msg := <-reader:
				if err := session.Send(msg.(string)); err != nil {
					return
				}
			}

		}
	}()
	for {
		if msg, err := session.Recv(); err == nil {
			chat.Publish(msg)
			continue
		}
		break
	}
	close(closedSession)
	log.Println("sockjs session closed")
}
开发者ID:wavded,项目名称:sockjs-go,代码行数:29,代码来源:webchat.go

示例2: send

func send(s sockjs.Session, data interface{}) error {
	b, err := json.Marshal(data)
	if err != nil {
		return err
	}

	return s.Send(string(b))
}
开发者ID:kellegous,项目名称:404,代码行数:8,代码来源:hub.go

示例3: echoHandler

func echoHandler(session sockjs.Session) {
	log.Println("new sockjs session established")
	for {
		if msg, err := session.Recv(); err == nil {
			session.Send(msg)
			continue
		}
		break
	}
	log.Println("sockjs session closed")
}
开发者ID:wavded,项目名称:sockjs-go,代码行数:11,代码来源:webecho.go

示例4: respondError

func respondError(session sockjs.Session, message string) {
	result := cmd.Response{
		Status: "error",
		Result: message,
	}
	response, err := json.Marshal(result)
	if err != nil {
		panic(err)
	}
	session.Send(string(response))
}
开发者ID:matobet,项目名称:verdi,代码行数:11,代码来源:websocket.go

示例5: wsHandler

func wsHandler(session sockjs.Session) {
	log.Println("new sockjs session established")
	go func() {
		reader, _ := broadcaster.SubChannel(nil)
		for {
			status := <-reader
			if err := session.Send(status.(string)); err != nil {
				return
			}
		}
	}()
}
开发者ID:nh0815,项目名称:golem,代码行数:12,代码来源:main.go

示例6: echoHandler

func echoHandler(conn sockjs.Session) {
	log.Println("New connection created")
	for {
		if msg, err := conn.Recv(); err != nil {
			break
		} else {
			if err := conn.Send(msg); err != nil {
				break
			}
		}
	}
	log.Println("Sessionection closed")
}
开发者ID:eelcocramer,项目名称:sockjs-go,代码行数:13,代码来源:server.go

示例7: AddClient

func (this *Room) AddClient(client sockjs.Session) {
	if this.Len() >= this.max {
		return
	} else if indexOf(this.clients, client) == -1 {
		message := "{\"type\":\"userConnected\"}"
		this.sendToAll(message)

		for i := 0; i < this.Len(); i++ {
			client.Send(message)
		}

		this.clients = append(this.clients, client)
	}
}
开发者ID:nickalie,项目名称:GoSockJSExample,代码行数:14,代码来源:room.go

示例8: Handler

func (ws *Ws) Handler(session sockjs.Session) {
	for {
		if msg, err := session.Recv(); err == nil {
			log.Printf("ws: received: %v", msg)
			message := map[string]interface{}{}
			err = json.Unmarshal([]byte(msg), &message)
			if err != nil {
				respondError(session, err.Error())
				continue
			}
			command, ok := message["cmd"]
			if !ok {
				respondError(session, "`cmd` field must be specified!")
				continue
			}
			commandName, ok := command.(string)
			if !ok {
				respondError(session, "`cmd` must be a string")
				continue
			}
			params, ok := message["params"]
			if !ok {
				respondError(session, "`params` field must be specified!")
				continue
			}
			paramsMap, ok := params.(map[string]interface{})
			if !ok {
				respondError(session, "`params` must be an object!")
				continue
			}
			result, err := ws.Run(commandName, paramsMap)
			if err != nil {
				respondError(session, err.Error())
				continue
			}
			response, err := json.Marshal(result)
			if err != nil {
				panic(err)
			}
			session.Send(string(response))
		} else {
			log.Println(err)
			break
		}
	}
}
开发者ID:matobet,项目名称:verdi,代码行数:46,代码来源:websocket.go

示例9: echoHandler

func echoHandler(session sockjs.Session) {
	for {
		if msg, err := session.Recv(); err == nil {
			session.Send(msg)
			continue
		}
		break
	}
}
开发者ID:dnamenon,项目名称:webshop,代码行数:9,代码来源:routes.go

示例10: receiveMessage

func receiveMessage(session sockjs.Session) {
	reader, _ := pub.SubChannel(nil)
	for {
		select {
		case msg, ok := <-reader:
			if !ok {
				log.Println("channel closed")
				return
			}
			msg = msg.(*socketMessage)
			if body, err := json.Marshal(msg); err == nil {
				log.Println("message:", string(body))
				if err = session.Send(string(body)); err != nil {
					log.Println(err)
					return
				}
			}
		}
	}
}
开发者ID:sandbreaker,项目名称:photoshare,代码行数:20,代码来源:messages.go

示例11: echoHandler

func echoHandler(session sockjs.Session) {
	log.Println("new sockjs session established")
	var closedSession = make(chan struct{})
	session.Send(session.ID())
	go func() {
		reader, _ := chat.SubChannel(nil)
		for {
			select {
			case <-closedSession:
				return
			case msg := <-reader:
				if err := session.Send(msg.(string)); err != nil {
					return
				}
			}

		}
	}()
	for {
		if msg, err := session.Recv(); err == nil {
			data := &WSDataSchema{}
			buf := bytes.NewBufferString(msg)
			err = json.Unmarshal(buf.Bytes(), data)
			if err == nil {
				_type := data.Type
				_endpoint := data.Endpoint
				_id := data.Id
				if _type == "container" {
					session.Send(_endpoint + _id)
					break
				}
			}
			chat.Publish(msg)
			//session.Send(msg)
			continue
		}
		break
	}
	close(closedSession)
	log.Println("sockjs session closed")
}
开发者ID:nkwangleiGIT,项目名称:dockerboard,代码行数:41,代码来源:ws.go

示例12: Chat

func Chat(session sockjs.Session) {
	glog.Info("Session started")
	sessionId := session.ID()

	chatSession := &ChatSession{
		Id:      sessionId,
		IsReady: false,
		Session: &session,
	}

	ChatSessions.Set(sessionId, chatSession)

	acceptedTypes := &map[string]string{
		"image/jpeg": ".jpg",
		"image/jpg":  ".jpg",
		"image/png":  ".png",
		"image/gif":  ".gif",
	}

	ticker := time.NewTicker(time.Second)

	// Not too nice, will be refactored later...
	online := func() {
		ready, chatting := ChatSessions.GetNumberOfReadyAndChatting()

		msg := &ChatMessage{
			"event": "online",
			"r":     ready,
			"c":     chatting,
		}
		out, _ := json.Marshal(msg)
		body := string(out)
		session.Send(body)
	}

	go func() {
		for _ = range ticker.C {
			online()
		}
	}()
	online()

	for {
		if msg, err := session.Recv(); err == nil {
			var data ChatMessage
			json.Unmarshal([]byte(msg), &data)

			switch data["event"] {
			case "ready":
				ChatReady(sessionId, data["region"].(string))
			case "typing":
				ChatSessions.Action(func() {
					chatSession.Room.BroadcastOthers(sessionId, "typing", strconv.FormatBool(data["typing"].(bool)))
				})
			case "send":
				ChatSessions.Action(func() {
					chatSession.Room.BroadcastOthers(sessionId, "message", data["message"].(string))
				})
			case "exit":
				ChatSessions.Action(func() {
					glog.Info("Chat session ended")
					chatSession.Room.BroadcastOthers(sessionId, "exit", "")

					for i, _ := range chatSession.Room.Sessions {
						s := chatSession.Room.Sessions[i]
						if s != chatSession {
							s.Room = nil
						}
						//s.IsReady = true
					}
					chatSession.Room = nil
				})
			case "picture":
				glog.Info("Picture received")
				ChatSessions.Action(func() {
					chatSession.Room.BroadcastOthers(sessionId, "picturebefore", "true")
					dataURL, err := dataurl.DecodeString(data["data"].(string))
					if err != nil {
						glog.Error("Problem decoding file: ", err)
					}
					filename := helpers.GetRandomString(8)

					mt := dataURL.ContentType()

					if ext, ok := (*acceptedTypes)[mt]; ok {
						err = ioutil.WriteFile(MediaContent+filename+ext, dataURL.Data, 0644)
						if err != nil {
							glog.Error("Error saving file: ", err)
						}
						chatSession.Room.BroadcastOthers(sessionId, "picture", data["data"].(string))
					}
				})
			}

			continue
		}
		break
	}

	ticker.Stop()
//.........这里部分代码省略.........
开发者ID:rand99,项目名称:taillachat,代码行数:101,代码来源:chat.go

示例13: SendJsonMessage

func SendJsonMessage(session sockjs.Session, json string) {
	session.Send(json)
}
开发者ID:KingBright,项目名称:grouplay,代码行数:3,代码来源:msg.go


注:本文中的gopkg/in/igm/sockjs-go/v2/sockjs.Session.Send方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。