Skip to content

Commit

Permalink
added go modules to v2 (#88)
Browse files Browse the repository at this point in the history
* added go mod support for v2
* README points to master
* cleanup, fix of sockjs client library URL
  • Loading branch information
igm authored Apr 16, 2020
1 parent d82eeaf commit f490575
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 142 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
# Folders
_obj
_test
.idea

# Architecture specific extensions/prefixes
*.[568vq]
Expand Down
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
sudo: false
language: go

go:
- "1.13.x"
- "1.12.x"
- tip
- "1.14.x"

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 -race ./...
88 changes: 5 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,8 @@
[![Build Status](https://api.travis-ci.org/igm/sockjs-go.svg?branch=v2)](https://travis-ci.org/igm/sockjs-go) [![GoDoc](http://godoc.org/gopkg.in/igm/sockjs-go.v2/sockjs?status.png)](http://godoc.org/gopkg.in/igm/sockjs-go.v2/sockjs) [![Coverage Status](https://coveralls.io/repos/igm/sockjs-go/badge.png?branch=v2)](https://coveralls.io/r/igm/sockjs-go?branch=v2)

What is SockJS?
=

SockJS is a JavaScript library (for browsers) that provides a WebSocket-like
object. SockJS gives you a coherent, cross-browser, Javascript API
which creates a low latency, full duplex, cross-domain communication
channel between the browser and the web server, with WebSockets or without.
This necessitates the use of a server, which this is one version of, for GO.


SockJS-Go server library
=

SockJS-Go is a [SockJS](https://github.com/sockjs/sockjs-client) server library written in Go.

To use current stable version **v2**

go get gopkg.in/igm/sockjs-go.v2/sockjs

To use previous version **v1** (DEPRECATED)

go get gopkg.in/igm/sockjs-go.v1/sockjs

To install **development** version of `sockjs-go` run:

go get github.com/igm/sockjs-go/sockjs


Versioning
-

SockJS-Go project adopted [gopkg.in](http://gopkg.in) approach for versioning. SockJS-Go library details can be found [here](https://gopkg.in/igm/sockjs-go.v2/sockjs)


Example
v2
-

A simple echo sockjs server:


```go
package main

import (
"log"
"net/http"

"gopkg.in/igm/sockjs-go.v2/sockjs"
)

func main() {
handler := sockjs.NewHandler("/echo", sockjs.DefaultOptions, echoHandler)
log.Fatal(http.ListenAndServe(":8081", handler))
}

func echoHandler(session sockjs.Session) {
for {
if msg, err := session.Recv(); err == nil {
session.Send(msg)
continue
}
break
}
}
```


SockJS Protocol Tests Status
-
SockJS defines a set of [protocol tests](https://github.com/sockjs/sockjs-protocol) to quarantee a server compatibility with sockjs client library and various browsers. SockJS-Go server library aims to provide full compatibility, however there are couple of tests that don't and probably will never pass due to reasons explained in table below:


| Failing Test | Explanation |
| -------------| ------------|
| **XhrPolling.test_transport** | does not pass due to a feature in net/http that does not send content-type header in case of StatusNoContent response code (even if explicitly set in the code), [details](https://code.google.com/p/go/source/detail?r=902dc062bff8) |
| **WebSocket.** | Sockjs Go version supports RFC 6455, draft protocols hixie-76, hybi-10 are not supported |
| **JSONEncoding** | As menioned in [browser quirks](https://github.com/sockjs/sockjs-client#browser-quirks) section: "it's advisable to use only valid characters. Using invalid characters is a bit slower, and may not work with SockJS servers that have a proper Unicode support." Go lang has a proper Unicode support |
| **RawWebsocket.** | The sockjs protocol tests use old WebSocket client library (hybi-10) that does not support RFC 6455 properly |

WebSocket
-
As mentioned above sockjs-go library is compatible with RFC 6455. That means the browsers not supporting RFC 6455 are not supported properly. There are no plans to support draft versions of WebSocket protocol. The WebSocket support is based on [Gorilla web toolkit](http://www.gorillatoolkit.org/pkg/websocket) implementation of WebSocket.
[![Build Status](https://api.travis-ci.org/igm/sockjs-go.svg?branch=v2)](https://travis-ci.org/igm/sockjs-go)
[![GoDoc](http://godoc.org/gopkg.in/igm/sockjs-go.v2/sockjs?status.svg)](http://godoc.org/gopkg.in/igm/sockjs-go.v2/sockjs)
[![Coverage Status](https://coveralls.io/repos/igm/sockjs-go/badge.svg?branch=v2)](https://coveralls.io/r/igm/sockjs-go?branch=v2)

For detailed information about browser versions supporting RFC 6455 see this [wiki page](http://en.wikipedia.org/wiki/WebSocket#Browser_support).
For more information see [master README](https://github.com/igm/sockjs-go/blob/master/README.md).
15 changes: 0 additions & 15 deletions examples/webchat/Dockerfile

This file was deleted.

4 changes: 0 additions & 4 deletions examples/webchat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ $ 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
```

77 changes: 77 additions & 0 deletions examples/webchat/pubsub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// taken from https://github.com/igm/pubsub
package main

import (
"sync"
)

// Subscription Reader is used to read messages published by Publisher
type SubReader interface {
// Read operation blocks and waits for message from Publisher
Read() interface{}
}

// Publisher is used to publish messages. Can be directly created.
type Publisher struct {
m sync.Mutex
lastMsg *msg
}

type subscriber struct{ in chan *msg }

type msg struct {
val interface{}
next chan *msg
}

func newMsg(val interface{}) *msg { return &msg{val: val, next: make(chan *msg, 1)} }

// Publish publishes a message to all existing subscribers
func (p *Publisher) Publish(val interface{}) {
p.m.Lock()
defer p.m.Unlock()

msg := newMsg(val)
if p.lastMsg != nil {
p.lastMsg.next <- msg
}
p.lastMsg = msg
}

// SubReader returns a new reader for reading published messages and a last published message.
func (p *Publisher) SubReader() (reader SubReader, lastMsg interface{}) {
p.m.Lock()
defer p.m.Unlock()

if p.lastMsg == nil {
p.lastMsg = newMsg(nil)
}
return &subscriber{p.lastMsg.next}, p.lastMsg.val
}

// SubChannel returns a new channel for reading published messages and a last published message.
// If published messages equals (==) finalMsg then channel is closed afer putting message into channel.
func (p *Publisher) SubChannel(finalMsg interface{}) (msgChan <-chan interface{}, lastMsg interface{}) {
listener, cur := p.SubReader()
outch := make(chan interface{})
go listen(listener, outch, finalMsg)
return outch, cur
}

func listen(subscriber SubReader, ch chan interface{}, finalMsg interface{}) {
defer close(ch)
for {
state := subscriber.Read()
ch <- state
if state == finalMsg {
return
}
}
}

func (s *subscriber) Read() interface{} {
msg := <-s.in
s.in <- msg
s.in = msg.next
return msg.val
}
12 changes: 6 additions & 6 deletions examples/webchat/web/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
<script type="text/javascript" src="app.js"> </script>
<meta charset="UTF-8">
<title>Chat Web Example</title>
<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" />
Input text: <input id="input" focus="true"/>
<input type="submit" disabled="true" id="send" value="Send"/>
</form>
<br/>
Messages from server:</br>
Expand Down
3 changes: 1 addition & 2 deletions examples/webchat/webchat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"log"
"net/http"

"github.com/igm/pubsub"
"gopkg.in/igm/sockjs-go.v2/sockjs"
)

var chat pubsub.Publisher
var chat Publisher

func main() {
http.Handle("/echo/", sockjs.NewHandler("/echo", sockjs.DefaultOptions, echoHandler))
Expand Down
12 changes: 6 additions & 6 deletions examples/webecho/web/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="http://cdn.sockjs.org/sockjs-0.3.min.js"></script>
<script type="text/javascript" src="app.js"> </script>
<meta charset="UTF-8">
<title>Echo Web Example</title>
<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" />
Input text: <input id="input" focus="true"/>
<input type="submit" disabled="true" id="send" value="Send"/>
</form>
<br/>
Messages from server:</br>
Expand Down
8 changes: 8 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module gopkg.in/igm/sockjs-go.v2

go 1.14

require (
github.com/gorilla/websocket v1.4.2
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
)
7 changes: 7 additions & 0 deletions go.sum
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=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e h1:3G+cUijn7XD+S4eJFddp53Pv7+slrESplyjG25HgL+k=
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=
3 changes: 0 additions & 3 deletions sockjs/.gitignore

This file was deleted.

1 change: 0 additions & 1 deletion sockjs/benchmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ func BenchmarkMessages(b *testing.B) {
wg.Add(1)
go func(session int) {
reqc := 0
// req, _ := http.NewRequest("POST", server.URL+fmt.Sprintf("/echo/server/%d/xhr_streaming", session), nil)
req, _ := http.NewRequest("GET", server.URL+fmt.Sprintf("/echo/server/%d/eventsource", session), nil)
for {
reqc++
Expand Down
2 changes: 1 addition & 1 deletion sockjs/example_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package sockjs_test
import (
"net/http"

"github.com/igm/sockjs-go/sockjs"
"gopkg.in/igm/sockjs-go.v2/sockjs"
)

func ExampleNewHandler_simple() {
Expand Down
2 changes: 1 addition & 1 deletion sockjs/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Options struct {
var DefaultOptions = Options{
Websocket: true,
JSessionID: nil,
SockJSURL: "http://cdn.sockjs.org/sockjs-0.3.min.js",
SockJSURL: "//cdn.jsdelivr.net/npm/sockjs-client@1/dist/sockjs.min.js",
HeartbeatDelay: 25 * time.Second,
DisconnectDelay: 5 * time.Second,
ResponseLimit: 128 * 1024,
Expand Down
14 changes: 0 additions & 14 deletions sockjs/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,27 +48,13 @@ func TestSession_ConcurrentSend(t *testing.T) {
func TestSession_AttachReceiver(t *testing.T) {
session := newTestSession()
recv := &testReceiver{}
// recv := &mockRecv{
// _sendFrame: func(frame string) {
// if frame != "o" {
// t.Errorf("Incorrect open header received")
// }
// },
// _sendBulk: func(...string) {},
// }
if err := session.attachReceiver(recv); err != nil {
t.Errorf("Should not return error")
}
if session.state != sessionActive {
t.Errorf("Session in wrong state after receiver attached %d, should be %d", session.state, sessionActive)
}
session.detachReceiver()
// recv = &mockRecv{
// _sendFrame: func(frame string) {
// t.Errorf("No frame shold be send, got '%s'", frame)
// },
// _sendBulk: func(...string) {},
// }
if err := session.attachReceiver(recv); err != nil {
t.Errorf("Should not return error")
}
Expand Down

0 comments on commit f490575

Please sign in to comment.