Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
2,900 additions
and
20 deletions.
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,25 +1,19 @@ | ||
sudo: false | ||
language: go | ||
|
||
go: | ||
- "1.13.x" | ||
- "1.12.x" | ||
- "1.14.x" | ||
- tip | ||
|
||
notifications: | ||
email: | ||
- igor.mihalik+travis@gmail.com | ||
|
||
before_install: | ||
- go get golang.org/x/tools/cmd/cover | ||
- go get github.com/mattn/goveralls | ||
|
||
after_success: | ||
- go test ./sockjs -coverprofile=profile.out -covermode=count | ||
- go test ./... -coverprofile=profile.out -covermode=count | ||
- PATH=$HOME/gopath/bin:$PATH goveralls -coverprofile=profile.out -service=travis-ci | ||
|
||
install: | ||
- go get -d -v -t ./... | ||
|
||
script: | ||
- go test ./sockjs -v -race | ||
- go test -v -race ./... |
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,16 @@ | ||
# Chat Example | ||
|
||
Simple sockjs chat example. | ||
|
||
## Run | ||
```shell | ||
$ go run webchat.go | ||
``` | ||
Navigate using web browser: http://127.0.0.1:8080 | ||
Open multiple windows with the same URL and see how chat works. | ||
|
||
## Docker | ||
```shell | ||
$ docker run -p=80:8080 -d imihalik/sockjs-chat | ||
``` | ||
|
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,22 @@ | ||
if (!window.location.origin) { // Some browsers (mainly IE) do not have this property, so we need to build it manually... | ||
window.location.origin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : ''); | ||
} | ||
|
||
|
||
var sock = new SockJS(window.location.origin+'/echo') | ||
|
||
sock.onopen = function() { | ||
// console.log('connection open'); | ||
document.getElementById("status").innerHTML = "connected"; | ||
document.getElementById("send").disabled=false; | ||
}; | ||
|
||
sock.onmessage = function(e) { | ||
document.getElementById("output").value += e.data +"\n"; | ||
}; | ||
|
||
sock.onclose = function() { | ||
// console.log('connection closed'); | ||
document.getElementById("status").innerHTML = "disconnected"; | ||
document.getElementById("send").disabled=true; | ||
}; |
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,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script> | ||
<script type="text/javascript" src="app.js"></script> | ||
<meta charset="UTF-8"> | ||
<title>Chat Web Example</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Chat - Web Example</h1> | ||
<form onSubmit='sock.send(document.getElementById("input").value); return false;'> | ||
Input text: <input id="input" focus="true"/> | ||
<input type="submit" disabled="true" id="send" value="Send"/> | ||
</form> | ||
<br/> | ||
Messages from server:</br> | ||
<textarea cols=80 rows=20 id="output"> | ||
</textarea> | ||
<br/> | ||
status: <span id="status">connecting...</span> | ||
|
||
</body> | ||
</html> |
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,48 @@ | ||
package main | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
|
||
"github.com/igm/pubsub" | ||
"github.com/igm/sockjs-go/v2/sockjs" | ||
) | ||
|
||
var chat pubsub.Publisher | ||
|
||
func main() { | ||
http.Handle("/echo/", sockjs.NewHandler("/echo", sockjs.DefaultOptions, echoHandler)) | ||
http.Handle("/", http.FileServer(http.Dir("web/"))) | ||
log.Println("Server started on port: 8080") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func echoHandler(session sockjs.Session) { | ||
log.Println("new sockjs session established") | ||
var closedSession = make(chan struct{}) | ||
chat.Publish("[info] new participant joined chat") | ||
defer chat.Publish("[info] participant left chat") | ||
go func() { | ||
reader, _ := chat.SubChannel(nil) | ||
for { | ||
select { | ||
case <-closedSession: | ||
return | ||
case msg := <-reader: | ||
if err := session.Send(msg.(string)); err != nil { | ||
return | ||
} | ||
} | ||
|
||
} | ||
}() | ||
for { | ||
if msg, err := session.Recv(); err == nil { | ||
chat.Publish(msg) | ||
continue | ||
} | ||
break | ||
} | ||
close(closedSession) | ||
log.Println("sockjs session closed") | ||
} |
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,9 @@ | ||
# Echo Example | ||
|
||
Simple echo sockjs example. | ||
|
||
## Run | ||
```shell | ||
$ go run webecho.go | ||
``` | ||
Navigate using web browser: http://127.0.0.1:8080 |
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,34 @@ | ||
if (!window.location.origin) { // Some browsers (mainly IE) do not have this property, so we need to build it manually... | ||
window.location.origin = window.location.protocol + '//' + window.location.hostname + (window.location.port ? (':' + window.location.port) : ''); | ||
} | ||
|
||
var origin = window.location.origin; | ||
|
||
// options usage example | ||
var options = { | ||
debug: true, | ||
devel: true, | ||
protocols_whitelist: ['websocket', 'xdr-streaming', 'xhr-streaming', 'iframe-eventsource', 'iframe-htmlfile', 'xdr-polling', 'xhr-polling', 'iframe-xhr-polling', 'jsonp-polling'] | ||
}; | ||
|
||
var sock = new SockJS(origin+'/echo', undefined, options); | ||
|
||
sock.onopen = function() { | ||
//console.log('connection open'); | ||
document.getElementById("status").innerHTML = "connected"; | ||
document.getElementById("send").disabled=false; | ||
}; | ||
|
||
sock.onmessage = function(e) { | ||
document.getElementById("output").value += e.data +"\n"; | ||
}; | ||
|
||
sock.onclose = function() { | ||
document.getElementById("status").innerHTML = "connection closed"; | ||
//console.log('connection closed'); | ||
}; | ||
|
||
function send() { | ||
text = document.getElementById("input").value; | ||
sock.send(document.getElementById("input").value); return false; | ||
} |
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,24 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<script type="text/javascript" src="//cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js"></script> | ||
<script type="text/javascript" src="app.js"></script> | ||
<meta charset="UTF-8"> | ||
<title>Echo Web Example</title> | ||
</head> | ||
|
||
<body> | ||
<h1>Echo - Web Example</h1> | ||
<form onSubmit='sock.send(document.getElementById("input").value); return false;'> | ||
Input text: <input id="input" focus="true"/> | ||
<input type="submit" disabled="true" id="send" value="Send"/> | ||
</form> | ||
<br/> | ||
Messages from server:</br> | ||
<textarea cols=80 rows=20 id="output"> | ||
</textarea> | ||
<br/> | ||
status: <span id="status">connecting...</span> | ||
|
||
</body> | ||
</html> |
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,39 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/igm/sockjs-go/v2/sockjs" | ||
) | ||
|
||
var ( | ||
websocket = flag.Bool("websocket", true, "enable/disable websocket protocol") | ||
) | ||
|
||
func init() { | ||
flag.Parse() | ||
} | ||
|
||
func main() { | ||
opts := sockjs.DefaultOptions | ||
opts.Websocket = *websocket | ||
handler := sockjs.NewHandler("/echo", opts, echoHandler) | ||
http.Handle("/echo/", handler) | ||
http.Handle("/", http.FileServer(http.Dir("web/"))) | ||
log.Println("Server started on port: 8080") | ||
log.Fatal(http.ListenAndServe(":8080", nil)) | ||
} | ||
|
||
func echoHandler(session sockjs.Session) { | ||
log.Println("new sockjs session established") | ||
for { | ||
if msg, err := session.Recv(); err == nil { | ||
session.Send(msg) | ||
continue | ||
} | ||
break | ||
} | ||
log.Println("sockjs session closed") | ||
} |
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,9 @@ | ||
module github.com/igm/sockjs-go/v2 | ||
|
||
go 1.14 | ||
|
||
require ( | ||
github.com/gorilla/websocket v1.4.2 | ||
github.com/igm/pubsub v1.0.0 | ||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e | ||
) |
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,7 @@ | ||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
github.com/igm/pubsub v1.0.0/go.mod h1:q4OjwAiOVflOQD1dXrh3QfJ+IqTG/xiQIvZwDeQIiqY= | ||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= | ||
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= | ||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | ||
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= |
Oops, something went wrong.