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
4 changed files
with
70 additions
and
4 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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,39 @@ | |||
package main | |||
|
|||
import ( | |||
"code.google.com/p/go.net/websocket" | |||
"github.com/stesla/gotelnet" | |||
"io" | |||
"net/http" | |||
) | |||
|
|||
func ConnectServer(w http.ResponseWriter, r *http.Request) { | |||
// TODO: do some sort of argument parsing here | |||
conn, err := gotelnet.Dial("localhost:2860") | |||
if err != nil { | |||
w.WriteHeader(500); | |||
return | |||
} | |||
websocket.Handler(func(ws *websocket.Conn) { | |||
forward(ws, conn); | |||
}).ServeHTTP(w, r) | |||
} | |||
|
|||
func forward(down *websocket.Conn, up gotelnet.Conn) { | |||
lineEnding := "\n"; | |||
exit := make(chan bool) | |||
go func() { io.Copy(down, up); exit <- true }() | |||
go func() { | |||
for { | |||
var msg string | |||
if rerr := websocket.Message.Receive(down, &msg); rerr != nil { | |||
break | |||
} | |||
if _, werr := up.Write([]byte(msg + lineEnding)); werr != nil { | |||
break | |||
} | |||
} | |||
exit <- true | |||
}() | |||
<-exit | |||
} |
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,14 +1,39 @@ | |||
(function() { | (function() { | ||
var input_, output_, ws_; | |||
|
|||
function connect(fn) { | |||
//TODO: wss when we're https | |||
var ws = new WebSocket("ws://" + window.location.host + "/connect"); | |||
ws.onopen = function() { | |||
fn("Connected!"); | |||
} | |||
ws.onclose = function() { | |||
fn("Disconnected!"); | |||
} | |||
ws.onmessage = function(msg) { | |||
fn(msg.data); | |||
} | |||
return ws; | |||
} | |||
|
|||
function processInput(evt) { | function processInput(evt) { | ||
if (this.value && evt.keyCode == 13) { | if (this.value && evt.keyCode == 13) { | ||
evt.preventDefault(); | evt.preventDefault(); | ||
console.log("input:", this.value); | ws_.send(this.value); | ||
this.value = ""; | console.log("sent:", this.value); | ||
this.value = ""; | |||
} | } | ||
} | } | ||
|
|
||
window.onload = function() { | window.onload = function() { | ||
var input = document.querySelector('#container .input textarea'); | input_ = document.querySelector('#container .input textarea'); | ||
input.addEventListener('keydown', processInput); | output_ = document.querySelector('#container .output'); | ||
input_.addEventListener('keydown', processInput); | |||
ws_ = connect(function(text) { | |||
var line = document.createElement('p'); | |||
line.innerHTML = text; | |||
output_.appendChild(line); | |||
input_.scrollIntoView(); | |||
}); | |||
}; | }; | ||
})(); | })(); |
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