Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
B
beego
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
beego
Commits
3ad639e7
Commit
3ad639e7
authored
Apr 07, 2013
by
astaxie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
support websocket #20
add example chat
parent
25736968
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
221 additions
and
1 deletion
+221
-1
beego.go
beego.go
+5
-0
chat.go
example/chat/chat.go
+42
-0
index.html
example/chat/views/index.html
+82
-0
router.go
router.go
+92
-1
No files found.
beego.go
View file @
3ad639e7
...
@@ -207,6 +207,11 @@ func Router(path string, c ControllerInterface) *App {
...
@@ -207,6 +207,11 @@ func Router(path string, c ControllerInterface) *App {
return
BeeApp
return
BeeApp
}
}
func
RouterHandler
(
path
string
,
c
http
.
Handler
)
*
App
{
BeeApp
.
Handlers
.
AddHandler
(
path
,
c
)
return
BeeApp
}
func
Filter
(
filter
http
.
HandlerFunc
)
*
App
{
func
Filter
(
filter
http
.
HandlerFunc
)
*
App
{
BeeApp
.
Filter
(
filter
)
BeeApp
.
Filter
(
filter
)
return
BeeApp
return
BeeApp
...
...
example/chat/chat.go
0 → 100644
View file @
3ad639e7
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
()
}
example/chat/views/index.html
0 → 100644
View file @
3ad639e7
<!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>
\ No newline at end of file
router.go
View file @
3ad639e7
...
@@ -16,14 +16,22 @@ type controllerInfo struct {
...
@@ -16,14 +16,22 @@ type controllerInfo struct {
controllerType
reflect
.
Type
controllerType
reflect
.
Type
}
}
type
userHandler
struct
{
pattern
string
regex
*
regexp
.
Regexp
params
map
[
int
]
string
h
http
.
Handler
}
type
ControllerRegistor
struct
{
type
ControllerRegistor
struct
{
routers
[]
*
controllerInfo
routers
[]
*
controllerInfo
fixrouters
[]
*
controllerInfo
fixrouters
[]
*
controllerInfo
filters
[]
http
.
HandlerFunc
filters
[]
http
.
HandlerFunc
userHandlers
map
[
string
]
*
userHandler
}
}
func
NewControllerRegistor
()
*
ControllerRegistor
{
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
)
{
func
(
p
*
ControllerRegistor
)
Add
(
pattern
string
,
c
ControllerInterface
)
{
...
@@ -77,6 +85,52 @@ 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.
// Filter adds the middleware filter.
func
(
p
*
ControllerRegistor
)
Filter
(
filter
http
.
HandlerFunc
)
{
func
(
p
*
ControllerRegistor
)
Filter
(
filter
http
.
HandlerFunc
)
{
p
.
filters
=
append
(
p
.
filters
,
filter
)
p
.
filters
=
append
(
p
.
filters
,
filter
)
...
@@ -143,6 +197,43 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
...
@@ -143,6 +197,43 @@ func (p *ControllerRegistor) ServeHTTP(rw http.ResponseWriter, r *http.Request)
requestPath
:=
r
.
URL
.
Path
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
//first find path from the fixrouters to Improve Performance
for
_
,
route
:=
range
p
.
fixrouters
{
for
_
,
route
:=
range
p
.
fixrouters
{
n
:=
len
(
requestPath
)
n
:=
len
(
requestPath
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment