-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Some extra documentation and a go-node interop example #5
Open
daviddias
wants to merge
6
commits into
spdy-http2:master
Choose a base branch
from
daviddias:docs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b167ce6
.gitignore
daviddias 73af706
Merge branch 'master' of github.com:indutny/spdy-transport
daviddias 6e56de3
add client documentation to the readme, frame listener and go-node in…
daviddias 2cbef27
add implementation notes section
daviddias b0e4d5e
improve the comment for client.start
daviddias 407d5ca
remove .on('readable'), add link to streams API, add semicollons to f…
daviddias File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
npm-debug.log | ||
.vimrc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/docker/spdystream" | ||
"net" | ||
"net/http" | ||
) | ||
|
||
func main() { | ||
conn, err := net.Dial("tcp", "localhost:9090") | ||
if err != nil { | ||
panic(err) | ||
} | ||
spdyConn, err := spdystream.NewConnection(conn, false) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
header := http.Header{} | ||
// Although spdystream doesn't mind about :method and :path headers, these | ||
// are necessary for interop with spdy-transport | ||
header.Add(":method", "GET") | ||
header.Add(":path", "/") | ||
|
||
go spdyConn.Serve(spdystream.NoOpStreamHandler) | ||
|
||
stream, err := spdyConn.CreateStream(header, nil, false) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
stream.Wait() | ||
|
||
fmt.Fprint(stream, "Hey, how is it going! (go client asking)") | ||
|
||
buf := make([]byte, 35) | ||
stream.Read(buf) | ||
fmt.Println(string(buf)) | ||
|
||
stream.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
var tcp = require('net') | ||
var transport = require('spdy-transport') | ||
|
||
var socket = tcp.connect({port: 9090}, function () { | ||
|
||
var client = transport.connection.create(socket, { | ||
protocol: 'spdy', | ||
isServer: false | ||
}) | ||
|
||
client.on('frame', function (frame) { | ||
console.log(frame.type) | ||
}) | ||
|
||
client.start(3.1) | ||
|
||
client.request({ | ||
method: 'GET', | ||
host: 'localhost', | ||
path: '/', | ||
headers: { | ||
a: 'b' | ||
}}, function (err, stream) { | ||
console.log('stream established') | ||
if (err) { | ||
return console.log(err) | ||
} | ||
stream.write('Hey, how is it going. (node client asking)!') | ||
|
||
stream.on('readable', function () { | ||
var chunk = stream.read() | ||
if (!chunk) { | ||
return | ||
} | ||
console.log(chunk.toString()) | ||
}) | ||
|
||
stream.on('response', function (code, headers) { | ||
console.log(code, headers) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/docker/spdystream" | ||
"net" | ||
) | ||
|
||
func main() { | ||
listener, err := net.Listen("tcp", "localhost:9090") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
for { | ||
conn, err := listener.Accept() | ||
if err != nil { | ||
panic(err) | ||
} | ||
spdyConn, err := spdystream.NewConnection(conn, true) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// This is an "echo server" example, known in the go world as "mirror" | ||
go spdyConn.Serve(spdystream.MirrorStreamHandler) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
var tcp = require('net') | ||
var transport = require('spdy-transport') | ||
|
||
tcp.createServer(function (socket) { | ||
var server = transport.connection.create(socket, { | ||
protocol: 'spdy', | ||
isServer: true | ||
}) | ||
|
||
server.on('stream', function (stream) { | ||
console.log(stream.method, stream.path, stream.headers) | ||
|
||
stream.respond(200, { | ||
c: 'd' | ||
}) | ||
stream.write('Hey, how is it going? (node server asking') | ||
|
||
stream.on('readable', function () { | ||
var chunk = stream.read() | ||
if (!chunk) { | ||
return | ||
} | ||
console.log(chunk.toString()) | ||
}) | ||
|
||
stream.on('end', function () { | ||
console.log('end') | ||
}) | ||
}) | ||
|
||
}).listen(9090) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is kind of internal thing, just for testing. Are you sure that it is worth putting it into readme?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, so how should we correctly read the body sent by the server?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use
stream.read()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it. Although I believe it might be a nice reminder that a good way to check if more bytes are available is through the .on('readable') event. Have seen a couple of times folks doing something like:
What if I left only the stream.read() and a comment with a note about the .on('readable') event?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... I'm not sure if this library is the right place to educate people on how the streams2 API should be used. I'd just link the node documentation if you think that this question is going to be raised often.