Skip to content

Commit ad72874

Browse files
Init.
0 parents  commit ad72874

File tree

1,786 files changed

+536003
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,786 files changed

+536003
-0
lines changed

LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) 2013 Mathieu Turcotte
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7+
of the Software, and to permit persons to whom the Software is furnished to do
8+
so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
About
2+
=====
3+
4+
This a server-side browser channel implementation written in Go.
5+
6+
The client-side javascript implementation is provided by the
7+
[Google Closure Library](https://developers.google.com/closure/library/).
8+
9+
Installing
10+
==========
11+
12+
$ go get github.com/MathieuTurcotte/go-browserchannel/bc
13+
14+
Documentation
15+
=============
16+
17+
Read it [online](http://go.pkgdoc.org/github.com/MathieuTurcotte/go-broswerchannel/bc) or run
18+
19+
$ go doc github.com/MathieuTurcotte/go-browserchannel/bc
20+
21+
License
22+
=======
23+
24+
This code is free to use under the terms of the [MIT license](http://mturcotte.mit-license.org/).

bc/backchannel.go

+127
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright (c) 2013 Mathieu Turcotte
2+
// Licensed under the MIT license.
3+
4+
package bc
5+
6+
import (
7+
"io"
8+
"log"
9+
"net/http"
10+
"strconv"
11+
)
12+
13+
// The back channel interface shared between the XHR and HTML implementations.
14+
type backChannel interface {
15+
getRequestId() string
16+
isReusable() bool
17+
setChunked(bool)
18+
isChunked() bool
19+
send(data []byte) (int, error)
20+
discard()
21+
wait()
22+
}
23+
24+
// Common bookeeping information shared between the chunked XHR and HTML
25+
// variants.
26+
type backChannelBase struct {
27+
sid SessionId
28+
rid string
29+
w http.ResponseWriter
30+
chunked bool
31+
bytesSent int
32+
done chan bool
33+
}
34+
35+
func (b *backChannelBase) getRequestId() string {
36+
return b.rid
37+
}
38+
39+
func (b *backChannelBase) isReusable() bool {
40+
return b.chunked && b.bytesSent < 10*1024
41+
}
42+
43+
func (b *backChannelBase) setChunked(chunked bool) {
44+
b.chunked = chunked
45+
}
46+
47+
func (b *backChannelBase) isChunked() bool {
48+
return b.chunked
49+
}
50+
51+
func (b *backChannelBase) wait() {
52+
<-b.done
53+
}
54+
55+
// The chunked XHR back channel implementation.
56+
type xhrBackChannel struct {
57+
backChannelBase
58+
}
59+
60+
func (b *xhrBackChannel) send(data []byte) (n int, err error) {
61+
log.Printf("%s:%s: xhr back channel send: %s\n", b.sid, b.rid, data)
62+
63+
n, err = io.WriteString(b.w,
64+
strconv.FormatInt(int64(len(data)), 10)+"\n"+string(data))
65+
66+
b.w.(http.Flusher).Flush()
67+
68+
if err == nil {
69+
b.bytesSent += n
70+
}
71+
72+
return
73+
}
74+
75+
func (b *xhrBackChannel) discard() {
76+
log.Printf("%s:%s: xhr back channel close\n", b.sid, b.rid)
77+
close(b.done)
78+
}
79+
80+
// The chunked HTML back channel implementation used for IE9 and older.
81+
type htmlBackChannel struct {
82+
backChannelBase
83+
paddingSent bool
84+
domain string
85+
}
86+
87+
func (b *htmlBackChannel) send(data []byte) (n int, err error) {
88+
log.Printf("%s:%s: html back channel send: %s\n", b.sid, b.rid, data)
89+
90+
if !b.paddingSent {
91+
writeHtmlHead(b.w)
92+
writeHtmlDomain(b.w, b.domain)
93+
}
94+
95+
n, err = writeHtmlRpc(b.w, string(data))
96+
97+
if !b.paddingSent {
98+
writeHtmlPadding(b.w)
99+
b.paddingSent = true
100+
}
101+
102+
b.w.(http.Flusher).Flush()
103+
104+
if err == nil {
105+
b.bytesSent += n
106+
}
107+
108+
return
109+
}
110+
111+
func (b *htmlBackChannel) discard() {
112+
log.Printf("%s:%s: html back channel close\n", b.sid, b.rid)
113+
writeHtmlDone(b.w)
114+
close(b.done)
115+
}
116+
117+
func newBackChannel(sid SessionId, w http.ResponseWriter, html bool,
118+
domain string, rid string) (bc backChannel) {
119+
base := backChannelBase{sid: sid, rid: rid, w: w, done: make(chan bool)}
120+
121+
if html {
122+
bc = &htmlBackChannel{backChannelBase: base, domain: domain}
123+
} else {
124+
bc = &xhrBackChannel{base}
125+
}
126+
return
127+
}

0 commit comments

Comments
 (0)