This repository was archived by the owner on Apr 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
feat(transport): Add websocket transport; #5
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6ecbe23
chain middlewares
salemzii ce78eb3
refactored go.mod
salemzii d55b056
refactored go.mod
salemzii 8d25292
chain multiple middlewares
salemzii 59e30f5
-- clean up
salemzii 84fdb09
included new transport: websockets
salemzii 90ca14a
made changes, updated websocket transport lib
salemzii 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 hidden or 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 hidden or 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 hidden or 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,132 @@ | ||
//Package rpc provides abstract rpc server | ||
// | ||
//Copyright (C) 2022 Alexander Kiryukhin <[email protected]> | ||
// | ||
//This file is part of go.neonxp.dev/jsonrpc2 project. | ||
// | ||
//This program is free software: you can redistribute it and/or modify | ||
//it under the terms of the GNU General Public License as published by | ||
//the Free Software Foundation, either version 3 of the License, or | ||
//(at your option) any later version. | ||
// | ||
//This program is distributed in the hope that it will be useful, | ||
//but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
//GNU General Public License for more details. | ||
// | ||
//You should have received a copy of the GNU General Public License | ||
//along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
package transport | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"crypto/tls" | ||
"errors" | ||
"log" | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
const ( | ||
// Time allowed to write a message to the peer. | ||
writeWait = 10 * time.Second | ||
// Time allowed to read the next pong message from the peer. | ||
pongWait = 60 * time.Second | ||
|
||
// Send pings to peer with this period. Must be less than pongWait. | ||
pingPeriod = (pongWait * 9) / 10 | ||
|
||
// Maximum message size allowed from peer. | ||
maxMessageSize = 512 | ||
) | ||
|
||
var ( | ||
neonxp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
newline = []byte{'\n'} | ||
space = []byte{' '} | ||
errUpgradingConn = errors.New("encountered error upgrading connection to websocket protocol") | ||
errStartingServer = errors.New("encountered error starting http server") | ||
) | ||
|
||
var upgrader = websocket.Upgrader{ | ||
ReadBufferSize: 1024, | ||
WriteBufferSize: 1024, | ||
CheckOrigin: func(r *http.Request) bool { return true }, | ||
} | ||
|
||
type WebSocket struct { | ||
Bind string | ||
TLS *tls.Config | ||
CORSOrigin string | ||
Parallel bool | ||
} | ||
|
||
func (ws *WebSocket) Run(ctx context.Context, resolver Resolver) error { | ||
srv := http.Server{ | ||
Addr: ws.Bind, | ||
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
|
||
wsconn, err := upgrader.Upgrade(w, r, nil) | ||
if err != nil { | ||
log.Println(err) | ||
neonxp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return | ||
} | ||
|
||
log.Println("successfully upgraded connection") | ||
neonxp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
defer func() { | ||
wsconn.Close() | ||
}() | ||
|
||
wsconn.SetReadLimit(maxMessageSize) | ||
wsconn.SetReadDeadline(time.Now().Add(pongWait)) | ||
wsconn.SetPongHandler(func(string) error { | ||
wsconn.SetReadDeadline(time.Now().Add(pongWait)) | ||
return nil | ||
}) | ||
|
||
for { | ||
// read message from connection | ||
messageType, message, err := wsconn.ReadMessage() | ||
if err != nil { | ||
if websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway, websocket.CloseAbnormalClosure) { | ||
log.Printf("error: %v", err) | ||
neonxp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
break | ||
} | ||
|
||
message = bytes.TrimSpace(bytes.Replace(message, newline, space, -1)) | ||
neonxp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
wsconn.SetWriteDeadline(time.Now().Add(writeWait)) | ||
|
||
// create writer object that implements io.WriterCloser interface | ||
// messageType is same as the messageType recieved from the connection | ||
w, err := wsconn.NextWriter(messageType) | ||
if err != nil { | ||
return | ||
} | ||
|
||
resolver.Resolve(ctx, bytes.NewBuffer(message), w, ws.Parallel) | ||
} | ||
}), | ||
|
||
BaseContext: func(l net.Listener) context.Context { | ||
return ctx | ||
}, | ||
} | ||
|
||
go func() { | ||
<-ctx.Done() | ||
srv.Close() | ||
}() | ||
|
||
if err := srv.ListenAndServe(); err != http.ErrServerClosed { | ||
log.Println(err) | ||
neonxp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return err | ||
} | ||
return nil | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.