multi channel chat demo
This commit is contained in:
@@ -1,17 +1,15 @@
|
|||||||
# melody
|
# melody [](https://travis-ci.org/olahol/melody) [](https://godoc.org/github.com/olahol/melody)
|
||||||
|
|
||||||
[](https://godoc.org/github.com/olahol/melody)
|
> :notes: Minimalist websocket framework for Go.
|
||||||
[](https://travis-ci.org/olahol/melody)
|
|
||||||
|
|
||||||
> :notes: Simple websocket framework for Go
|
|
||||||
|
|
||||||
Melody is websocket framework based on [github.com/gorilla/websocket](https://github.com/gorilla/websocket)
|
Melody is websocket framework based on [github.com/gorilla/websocket](https://github.com/gorilla/websocket)
|
||||||
that abstracts away the more tedious parts of handling websockets. Features include:
|
that abstracts away the tedious parts of handling websockets. It gets out of
|
||||||
|
your way so you can write real-time apps. Features include:
|
||||||
|
|
||||||
* [x] Timeouts for write and read.
|
* [x] Clear and easy interface similar to `net/http` or Gin.
|
||||||
* [x] Built-in ping/pong handling.
|
* [x] A simple way to broadcast to all or selected connected sessions.
|
||||||
* [x] Message buffer for connections making concurrent writing easy.
|
* [x] Message buffers making concurrent writing safe.
|
||||||
* [x] Simple broadcasting to all or selected sessions.
|
* [x] Automatic handling of ping/pong and session timeouts.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@@ -21,10 +19,10 @@ go get github.com/olahol/melody
|
|||||||
|
|
||||||
## [Example](https://github.com/olahol/melody/tree/master/examples)
|
## [Example](https://github.com/olahol/melody/tree/master/examples)
|
||||||
|
|
||||||
[Simple broadcasting chat server](https://github.com/olahol/melody/tree/master/examples/chat),
|
[Multi channel chat server](https://github.com/olahol/melody/tree/master/examples/multichat),
|
||||||
error handling left as en exercise for the developer.
|
error handling left as en exercise for the developer.
|
||||||
|
|
||||||
[](https://github.com/olahol/melody/tree/master/examples/chat)
|
[](https://github.com/olahol/melody/tree/master/examples/multichat)
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -43,16 +41,24 @@ func main() {
|
|||||||
http.ServeFile(c.Writer, c.Request, "index.html")
|
http.ServeFile(c.Writer, c.Request, "index.html")
|
||||||
})
|
})
|
||||||
|
|
||||||
r.GET("/ws", func(c *gin.Context) {
|
r.GET("/channel/:name", func(c *gin.Context) {
|
||||||
|
http.ServeFile(c.Writer, c.Request, "chan.html")
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/channel/:name/ws", func(c *gin.Context) {
|
||||||
m.HandleRequest(c.Writer, c.Request)
|
m.HandleRequest(c.Writer, c.Request)
|
||||||
})
|
})
|
||||||
|
|
||||||
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||||
m.Broadcast(msg)
|
m.BroadcastFilter(msg, func(q *melody.Session) bool {
|
||||||
|
return q.Request.URL.Path == s.Request.URL.Path
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
r.Run(":5000")
|
r.Run(":5000")
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### [Documentation](https://godoc.org/github.com/olahol/melody)
|
### [More examples](https://github.com/olahol/melody/tree/master/examples)
|
||||||
|
|
||||||
|
## [Documentation](https://godoc.org/github.com/olahol/melody)
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Melody example: chatting</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#chat {
|
||||||
|
text-align: left;
|
||||||
|
background: #f1f1f1;
|
||||||
|
width: 500px;
|
||||||
|
min-height: 300px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<center>
|
||||||
|
<h3 id="name"></h3>
|
||||||
|
<pre id="chat"></pre>
|
||||||
|
<input placeholder="say something" id="text" type="text">
|
||||||
|
</center>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var url = "ws://" + window.location.host + window.location.pathname + "/ws";
|
||||||
|
var ws = new WebSocket(url);
|
||||||
|
var name = "Guest" + Math.floor(Math.random() * 1000);
|
||||||
|
var channelName = window.location.pathname.split("/")[2];
|
||||||
|
|
||||||
|
document.getElementById("name").innerText = "Channel: " + channelName;
|
||||||
|
|
||||||
|
var chat = document.getElementById("chat");
|
||||||
|
var text = document.getElementById("text");
|
||||||
|
|
||||||
|
var now = function () {
|
||||||
|
var iso = new Date().toISOString();
|
||||||
|
return iso.split("T")[1].split(".")[0];
|
||||||
|
};
|
||||||
|
|
||||||
|
ws.onmessage = function (msg) {
|
||||||
|
var line = now() + " " + msg.data + "\n";
|
||||||
|
chat.innerText += line;
|
||||||
|
};
|
||||||
|
|
||||||
|
text.onkeydown = function (e) {
|
||||||
|
if (e.keyCode === 13 && text.value !== "") {
|
||||||
|
ws.send("<" + name + "> " + text.value);
|
||||||
|
text.value = "";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Binary file not shown.
|
After Width: | Height: | Size: 149 KiB |
@@ -0,0 +1,34 @@
|
|||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Melody example: chatting</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
#chat {
|
||||||
|
text-align: left;
|
||||||
|
background: #f1f1f1;
|
||||||
|
width: 500px;
|
||||||
|
min-height: 300px;
|
||||||
|
padding: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<center>
|
||||||
|
<h3>Join a channel</h3>
|
||||||
|
<input placeholder="channel" id="channel" type="text"><button id="join">Join</button>
|
||||||
|
</center>
|
||||||
|
|
||||||
|
<script>
|
||||||
|
var chan = document.getElementById("channel");
|
||||||
|
var join = document.getElementById("join");
|
||||||
|
|
||||||
|
join.onclick = function () {
|
||||||
|
if (chan.value != "") {
|
||||||
|
window.location = "/channel/" + chan.value;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"../../"
|
||||||
|
"github.com/gin-gonic/gin"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
r := gin.Default()
|
||||||
|
m := melody.New()
|
||||||
|
|
||||||
|
r.GET("/", func(c *gin.Context) {
|
||||||
|
http.ServeFile(c.Writer, c.Request, "index.html")
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/channel/:name", func(c *gin.Context) {
|
||||||
|
http.ServeFile(c.Writer, c.Request, "chan.html")
|
||||||
|
})
|
||||||
|
|
||||||
|
r.GET("/channel/:name/ws", func(c *gin.Context) {
|
||||||
|
m.HandleRequest(c.Writer, c.Request)
|
||||||
|
})
|
||||||
|
|
||||||
|
m.HandleMessage(func(s *melody.Session, msg []byte) {
|
||||||
|
m.BroadcastFilter(msg, func(q *melody.Session) bool {
|
||||||
|
return q.Request.URL.Path == s.Request.URL.Path
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
r.Run(":5000")
|
||||||
|
}
|
||||||
@@ -63,14 +63,14 @@ func (m *Melody) HandleError(fn func(*Session, error)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Handles a http request and upgrades it to a websocket.
|
// Handles a http request and upgrades it to a websocket.
|
||||||
func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error {
|
func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) {
|
||||||
conn, err := m.upgrader.Upgrade(w, r, nil)
|
conn, err := m.upgrader.Upgrade(w, r, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
session := newSession(m.Config, conn)
|
session := newSession(m.Config, conn, r)
|
||||||
|
|
||||||
m.hub.register <- session
|
m.hub.register <- session
|
||||||
|
|
||||||
@@ -83,8 +83,6 @@ func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error {
|
|||||||
m.hub.unregister <- session
|
m.hub.unregister <- session
|
||||||
|
|
||||||
go m.disconnectHandler(session)
|
go m.disconnectHandler(session)
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Broadcasts a message to all sessions.
|
// Broadcasts a message to all sessions.
|
||||||
@@ -94,7 +92,7 @@ func (m *Melody) Broadcast(msg []byte) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Broadcasts a message to all sessions that fn returns true for.
|
// Broadcasts a message to all sessions that fn returns true for.
|
||||||
func (m *Melody) BroadcastFilter(fn func(*Session) bool, msg []byte) {
|
func (m *Melody) BroadcastFilter(msg []byte, fn func(*Session) bool) {
|
||||||
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn}
|
message := &envelope{t: websocket.TextMessage, msg: msg, filter: fn}
|
||||||
m.hub.broadcast <- message
|
m.hub.broadcast <- message
|
||||||
}
|
}
|
||||||
|
|||||||
+4
-1
@@ -2,18 +2,21 @@ package melody
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
// A melody session.
|
// A melody session.
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
Request *http.Request
|
||||||
conn *websocket.Conn
|
conn *websocket.Conn
|
||||||
output chan *envelope
|
output chan *envelope
|
||||||
config *Config
|
config *Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSession(config *Config, conn *websocket.Conn) *Session {
|
func newSession(config *Config, conn *websocket.Conn, req *http.Request) *Session {
|
||||||
return &Session{
|
return &Session{
|
||||||
|
Request: req,
|
||||||
conn: conn,
|
conn: conn,
|
||||||
output: make(chan *envelope, config.MessageBufferSize),
|
output: make(chan *envelope, config.MessageBufferSize),
|
||||||
config: config,
|
config: config,
|
||||||
|
|||||||
Reference in New Issue
Block a user