blob: 96945f6d68856d1e530fce3362000a4a79003eb0 [file] [log] [blame]
David Symondsbbcdc9d2014-02-19 11:36:36 +11001// Copyright 2011 Google Inc. All rights reserved.
2// Use of this source code is governed by the Apache 2.0
3// license that can be found in the LICENSE file.
4
5/*
6Package channel implements the server side of App Engine's Channel API.
7
8Create creates a new channel associated with the given clientID,
9which must be unique to the client that will use the returned token.
10
11token, err := channel.Create(c, "player1")
12if err != nil {
13// handle error
14}
15// return token to the client in an HTTP response
16
17Send sends a message to the client over the channel identified by clientID.
18
19channel.Send(c, "player1", "Game over!")
Chris Broadfootd11fc8a2017-06-30 13:53:15 -070020
21Deprecated: The Channel API feature has been deprecated and is going to be removed. See the Channel API Turndown document for details and timetable.
22
23https://cloud.google.com/appengine/docs/deprecations/channel
David Symondsbbcdc9d2014-02-19 11:36:36 +110024*/
Dave Dayd510d6e2014-11-13 17:12:34 +110025package channel // import "google.golang.org/appengine/channel"
David Symondsbbcdc9d2014-02-19 11:36:36 +110026
27import (
28"encoding/json"
29
David Symonds1c3fdc52014-11-14 14:53:44 +110030"golang.org/x/net/context"
31
David Symondsbbcdc9d2014-02-19 11:36:36 +110032"google.golang.org/appengine"
33"google.golang.org/appengine/internal"
34basepb "google.golang.org/appengine/internal/base"
35pb "google.golang.org/appengine/internal/channel"
36)
37
38// Create creates a channel and returns a token for use by the client.
39// The clientID is an application-provided string used to identify the client.
David Symonds1c3fdc52014-11-14 14:53:44 +110040func Create(c context.Context, clientID string) (token string, err error) {
David Symondsbbcdc9d2014-02-19 11:36:36 +110041req := &pb.CreateChannelRequest{
42ApplicationKey: &clientID,
43}
44resp := &pb.CreateChannelResponse{}
David Symondsd1e7e222015-01-28 16:44:06 +110045err = internal.Call(c, service, "CreateChannel", req, resp)
David Symondsbbcdc9d2014-02-19 11:36:36 +110046token = resp.GetToken()
47return token, remapError(err)
48}
49
50// Send sends a message on the channel associated with clientID.
David Symonds1c3fdc52014-11-14 14:53:44 +110051func Send(c context.Context, clientID, message string) error {
David Symondsbbcdc9d2014-02-19 11:36:36 +110052req := &pb.SendMessageRequest{
53ApplicationKey: &clientID,
54Message: &message,
55}
56resp := &basepb.VoidProto{}
David Symondsd1e7e222015-01-28 16:44:06 +110057return remapError(internal.Call(c, service, "SendChannelMessage", req, resp))
David Symondsbbcdc9d2014-02-19 11:36:36 +110058}
59
60// SendJSON is a helper function that sends a JSON-encoded value
61// on the channel associated with clientID.
David Symonds1c3fdc52014-11-14 14:53:44 +110062func SendJSON(c context.Context, clientID string, value interface{}) error {
David Symondsbbcdc9d2014-02-19 11:36:36 +110063m, err := json.Marshal(value)
64if err != nil {
65return err
66}
67return Send(c, clientID, string(m))
68}
69
70// remapError fixes any APIError referencing "xmpp" into one referencing "channel".
71func remapError(err error) error {
72if e, ok := err.(*internal.APIError); ok {
73if e.Service == "xmpp" {
74e.Service = "channel"
75}
76}
77return err
78}
79
80var service = "xmpp" // prod
81
82func init() {
83if appengine.IsDevAppServer() {
84service = "channel" // dev
85}
86internal.RegisterErrorCodeMap("channel", pb.ChannelServiceError_ErrorCode_name)
87}