1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 15:50:49 +00:00

support websocket #20

add example chat
This commit is contained in:
astaxie 2013-04-08 00:28:32 +08:00
parent 2573696860
commit 3ad639e739
4 changed files with 223 additions and 4 deletions

View File

@ -207,6 +207,11 @@ func Router(path string, c ControllerInterface) *App {
return BeeApp
}
func RouterHandler(path string, c http.Handler) *App {
BeeApp.Handlers.AddHandler(path, c)
return BeeApp
}
func Filter(filter http.HandlerFunc) *App {
BeeApp.Filter(filter)
return BeeApp

42
example/chat/chat.go Normal file
View File

@ -0,0 +1,42 @@
package main
import (
"fmt"
"github.com/astaxie/beego"
"github.com/fzzy/sockjs-go/sockjs"
"strings"
)
var users *sockjs.SessionPool = sockjs.NewSessionPool()
func chatHandler(s sockjs.Session) {
users.Add(s)
defer users.Remove(s)
for {
m := s.Receive()
if m == nil {
break
}
fullAddr := s.Info().RemoteAddr
addr := fullAddr[:strings.LastIndex(fullAddr, ":")]
m = []byte(fmt.Sprintf("%s: %s", addr, m))
users.Broadcast(m)
}
}
type MainController struct {
beego.Controller
}
func (m *MainController) Get() {
m.TplNames = "index.html"
}
func main() {
conf := sockjs.NewConfig()
sockjshandler := sockjs.NewHandler("/chat", chatHandler, conf)
beego.Router("/", &MainController{})
beego.RouterHandler("/chat/:info(.*)", sockjshandler)
beego.Run()
}

View File

@ -0,0 +1,81 @@
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://cdn.sockjs.org/sockjs-0.3.4.min.js"></script>
<script>
$(function() {
var conn = null;
function log(msg) {
var control = $('#log');
control.html(control.html() + msg + '<br/>');
control.scrollTop(control.scrollTop() + 1000);
}
function disconnect() {
if (conn != null) {
log('Disconnecting...');
conn.close();
conn = null;
updateUi();
}
}
function updateUi() {
if (conn == null || conn.readyState != SockJS.OPEN) {
$('#status').text('disconnected');
$('#connect').text('Connect');
} else {
$('#status').text('connected (' + conn.protocol + ')');
$('#connect').text('Disconnect');
}
}
$('form').submit(function() {
var text = $('#message').val();
conn.send(text);
$('#message').val('').focus();
return false;
});
conn = new SockJS('http://' + window.location.host + '/chat');
log('Connecting...');
conn.onopen = function() {
log('Connected.');
updateUi();
};
conn.onmessage = function(e) {
log(e.data);
};
conn.onclose = function() {
log('Disconnected.');
conn = null;
updateUi();
};
$('#message').val('').focus();
});
</script>
<title>Sockjs-go chat</title>
</head>
<body>
<h1>Sockjs-go chat</h1>
<div>
Status: <span id="status">disconnected</span>
</div>
<div id="log" style="width: 60em; height: 20em; overflow:auto; border: 1px solid black">
</div>
<form id="chatform">
<label for="message">Message:</label>
<input id="message" type="text" />
<input type="submit" value="Send" />
</form>
</body>
</html>

View File

@ -16,14 +16,22 @@ type controllerInfo struct {
controllerType reflect.Type
}
type userHandler struct {
pattern string
regex *regexp.Regexp
params map[int]string
h http.Handler
}
type ControllerRegistor struct {
routers []*controllerInfo
fixrouters []*controllerInfo
filters []http.HandlerFunc
routers []*controllerInfo
fixrouters []*controllerInfo
filters []http.HandlerFunc
userHandlers map[string]*userHandler
}
func NewControllerRegistor() *ControllerRegistor {
return &ControllerRegistor{routers: make([]*controllerInfo, 0)}
return &ControllerRegistor{routers: make([]*controllerInfo, 0), userHandlers: make(map[string]*userHandler)}
}
func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
@ -77,6 +85,52 @@ func (p *ControllerRegistor) Add(pattern string, c ControllerInterface) {
}
func (p *ControllerRegistor) AddHandler(pattern string, c http.Handler) {
parts := strings.Split(pattern, "/")
j := 0
params := make(map[int]string)
for i, part := range parts {
if strings.HasPrefix(part, ":") {
expr := "([^/]+)"
//a user may choose to override the defult expression
// similar to expressjs: /user/:id([0-9]+)
if index := strings.Index(part, "("); index != -1 {
expr = part[index:]
part = part[:index]
}
params[j] = part
parts[i] = expr
j++
}
}
if j == 0 {
//now create the Route
uh := &userHandler{}
uh.pattern = pattern
uh.h = c
p.userHandlers[pattern] = uh
} else { // add regexp routers
//recreate the url pattern, with parameters replaced
//by regular expressions. then compile the regex
pattern = strings.Join(parts, "/")
regex, regexErr := regexp.Compile(pattern)
if regexErr != nil {
//TODO add error handling here to avoid panic
panic(regexErr)
return
}
//now create the Route
uh := &userHandler{}
uh.regex = regex
uh.params = params
uh.pattern = pattern
uh.h = c
p.userHandlers[pattern] = uh
}
}
// Filter adds the middleware filter.
func (p *ControllerRegistor) Filter(filter http.HandlerFunc) {
p.filters = append(p.filters, filter)
@ -143,6 +197,43 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
requestPath := r.URL.Path
//user defined Handler
for pattern, c := range p.userHandlers {
if c.regex == nil && pattern == requestPath {
c.h.ServeHTTP(rw, r)
return
} else if c.regex == nil {
continue
}
//check if Route pattern matches url
if !c.regex.MatchString(requestPath) {
continue
}
//get submatches (params)
matches := c.regex.FindStringSubmatch(requestPath)
//double check that the Route matches the URL pattern.
if len(matches[0]) != len(requestPath) {
continue
}
if len(c.params) > 0 {
//add url parameters to the query param map
values := r.URL.Query()
for i, match := range matches[1:] {
values.Add(c.params[i], match)
params[c.params[i]] = match
}
//reassemble query params and add to RawQuery
r.URL.RawQuery = url.Values(values).Encode() + "&" + r.URL.RawQuery
//r.URL.RawQuery = url.Values(values).Encode()
}
c.h.ServeHTTP(rw, r)
return
}
//first find path from the fixrouters to Improve Performance
for _, route := range p.fixrouters {
n := len(requestPath)