David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 1 | // 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 | /* |
| 6 | Package channel implements the server side of App Engine's Channel API. |
| 7 | |
| 8 | Create creates a new channel associated with the given clientID, |
| 9 | which must be unique to the client that will use the returned token. |
| 10 | |
| 11 | token, err := channel.Create(c, "player1") |
| 12 | if err != nil { |
| 13 | // handle error |
| 14 | } |
| 15 | // return token to the client in an HTTP response |
| 16 | |
| 17 | Send sends a message to the client over the channel identified by clientID. |
| 18 | |
| 19 | channel.Send(c, "player1", "Game over!") |
Chris Broadfoot | d11fc8a | 2017-06-30 13:53:15 -0700 | [diff] [blame^] | 20 | |
| 21 | Deprecated: The Channel API feature has been deprecated and is going to be removed. See the Channel API Turndown document for details and timetable. |
| 22 | |
| 23 | https://cloud.google.com/appengine/docs/deprecations/channel |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 24 | */ |
Dave Day | d510d6e | 2014-11-13 17:12:34 +1100 | [diff] [blame] | 25 | package channel // import "google.golang.org/appengine/channel" |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 26 | |
| 27 | import ( |
| 28 | "encoding/json" |
| 29 | |
David Symonds | 1c3fdc5 | 2014-11-14 14:53:44 +1100 | [diff] [blame] | 30 | "golang.org/x/net/context" |
| 31 | |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 32 | "google.golang.org/appengine" |
| 33 | "google.golang.org/appengine/internal" |
| 34 | basepb "google.golang.org/appengine/internal/base" |
| 35 | pb "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 Symonds | 1c3fdc5 | 2014-11-14 14:53:44 +1100 | [diff] [blame] | 40 | func Create(c context.Context, clientID string) (token string, err error) { |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 41 | req := &pb.CreateChannelRequest{ |
| 42 | ApplicationKey: &clientID, |
| 43 | } |
| 44 | resp := &pb.CreateChannelResponse{} |
David Symonds | d1e7e22 | 2015-01-28 16:44:06 +1100 | [diff] [blame] | 45 | err = internal.Call(c, service, "CreateChannel", req, resp) |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 46 | token = resp.GetToken() |
| 47 | return token, remapError(err) |
| 48 | } |
| 49 | |
| 50 | // Send sends a message on the channel associated with clientID. |
David Symonds | 1c3fdc5 | 2014-11-14 14:53:44 +1100 | [diff] [blame] | 51 | func Send(c context.Context, clientID, message string) error { |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 52 | req := &pb.SendMessageRequest{ |
| 53 | ApplicationKey: &clientID, |
| 54 | Message: &message, |
| 55 | } |
| 56 | resp := &basepb.VoidProto{} |
David Symonds | d1e7e22 | 2015-01-28 16:44:06 +1100 | [diff] [blame] | 57 | return remapError(internal.Call(c, service, "SendChannelMessage", req, resp)) |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // SendJSON is a helper function that sends a JSON-encoded value |
| 61 | // on the channel associated with clientID. |
David Symonds | 1c3fdc5 | 2014-11-14 14:53:44 +1100 | [diff] [blame] | 62 | func SendJSON(c context.Context, clientID string, value interface{}) error { |
David Symonds | bbcdc9d | 2014-02-19 11:36:36 +1100 | [diff] [blame] | 63 | m, err := json.Marshal(value) |
| 64 | if err != nil { |
| 65 | return err |
| 66 | } |
| 67 | return Send(c, clientID, string(m)) |
| 68 | } |
| 69 | |
| 70 | // remapError fixes any APIError referencing "xmpp" into one referencing "channel". |
| 71 | func remapError(err error) error { |
| 72 | if e, ok := err.(*internal.APIError); ok { |
| 73 | if e.Service == "xmpp" { |
| 74 | e.Service = "channel" |
| 75 | } |
| 76 | } |
| 77 | return err |
| 78 | } |
| 79 | |
| 80 | var service = "xmpp" // prod |
| 81 | |
| 82 | func init() { |
| 83 | if appengine.IsDevAppServer() { |
| 84 | service = "channel" // dev |
| 85 | } |
| 86 | internal.RegisterErrorCodeMap("channel", pb.ChannelServiceError_ErrorCode_name) |
| 87 | } |