Comments (6)
@J7mbo sseserver.Server
implements the standard Go http.Handler
interface, so you should be able to integrate it directly into whatever HTTP mux you set up. Basically just treat it like any other http.Handler
, instead of calling it's Serve()
convenience function.
For example something like:
s := sseserver.NewServer()
http.Handle("/route", s)
err := http.ListenAndServeTLS(":8443", "cert.pem", "key.pem", nil)
if err != nil {
log.Fatal(err)
}
Make sense?
from sseserver.
@mroth Thanks for replying! Theoretically I should just be able to add the two lines for initialising the new sseserver and then handling a new route, but it's not working for me.
Your http.ListenAndServeTLS
api looks slightly different for me, I only pass two arguments.
I removed self-signed cert stuff for brevity but please let me know if you will find the more complete code useful:
server := &http.Server{
Handler: handlers.RecoveryHandler()(&router), // router is a mux.router
TLSConfig: &tls.Config{InsecureSkipVerify: true, RootCAs: rootCAs }
}
s := sseserver.NewServer()
http.Handle("/events", s)
return server.ListenAndServeTLS("cert.pem", "key.pem")
Here's my javascript to listen:
var e1 = new EventSource('https://localhost/events');
e1.onmessage = function(event) {
console.log(event);
};
I get no messages in my browser.
Edit: I also changed it to router.Handle("/events", s)
and it made no difference.
from sseserver.
Just in case I also added an event listener:
e1.addEventListener("events", function(event) {
console.log("GOT ONE");
});
But it looks like it's a 404 for localhost/events or something.
from sseserver.
So it initially looks like you are mixing different methodologies in the Go http standard library (which is admittedly fairly confusing). http.Handle
and http.ListenAndServe*
are convenience functions that operate on the DefaultServeMux
. Since you are using your own mux etc, you want to modify that instead.
Oh, hmm I see in your "Edit" you changed to having the mux router take the sseserver Handler, that should work. I'm not familiar with the RecoveryHandler
you are using here, but it looks to be part of the https://github.com/gorilla/handlers library, which claims to work with standard http.Handler
s, so I don't see why it would be a problem.
I guess paste the full code? Or if you can replicate the issue with minimal code.
from sseserver.
I created a minimal piece of code to replicate.
To get this working you need to generate your own certs. I used mkcert:
git clone https://github.com/FiloSottile/mkcert
cd mkcert && go build -ldflags "-X main.Version=$(git describe --tags)"
mkcert -install
mkcert -key-file ./certs/key.pem -cert-file ./certs/cert.pem localhost 127.0.0.1
Then I place these files in ./certs
. With the following code below I visit https://localhost
.
func main() {
// Router and html / js to listen for server-sent events.
router := mux.NewRouter()
router.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
writer.Header().Set("Content-Type", "text/html; charset=utf-8")
_, _ = writer.Write([]byte(`
Hello World
<script>
var e1 = new EventSource('https://localhost/events');
e1.onmessage = function(event) {
console.log(event);
};
e1.addEventListener("events", function(event) {
console.log(event);
});
</script>
`))
})
// Appending the gen'd cert.
rootCAs := x509.NewCertPool()
certs, _ := ioutil.ReadFile("./certs/cert.pem")
_ = rootCAs.AppendCertsFromPEM(certs)
// The SSE server, with /events handling it (this should then be https://localhost/events right??)
s := sseserver.NewServer()
router.Handle("/events", s)
server := &http.Server{
Handler: router,
TLSConfig: &tls.Config{
InsecureSkipVerify: true,
RootCAs: rootCAs,
},
}
// SSE every second.
go func() {
for {
msg := sseserver.SSEMessage{
Event: "events",
Data: []byte("TEST"),
Namespace: "",
}
s.Broadcast <- msg
time.Sleep(time.Duration(1) * time.Second)
}
}()
_ = server.ListenAndServeTLS("./certs/cert.pem", "./certs/key.pem")
}
from sseserver.
I think I've solved it but I'm not quite sure why the constraint exists...
The following works:
In Go:
router.Handle("/subscribe/time", s)
In Javascript:
new EventSource("https://localhost/subscribe/time");
The following does not work:
In Go:
router.Handle("/time", s)
In Javascript:
new EventSource("https://localhost/time");
I note that either way, the Namespace
field of SSEMessage
should contain /time
.
Is the /subscribe/
absolutely required, is this configurable?
Edit: Regardless of why, that's how it is. So thank you very much for the help and I'm sorry that this was basically completely my fault! Many thanks for your replies and thanks for this great library that integrates so nicely with the stdlib :-)
from sseserver.
Related Issues (11)
- add LICENSE information HOT 2
- Unable to run in a docker container HOT 3
- Catchup behaviour after sleeping HOT 1
- Upstream go.zipexe dependency issue causes panic in Docker container HOT 8
- move off deprecated azer/Debug dependency for debugging solution HOT 2
- Provide stats per subscription channel HOT 5
- consider renaming the project HOT 2
- cluster of sse HOT 3
- use sseserver with chi-router HOT 1
- Query on "Performance" information from README HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sseserver.