Browse Source

multi channel chat demo

master
Ola Holmström 10 years ago
parent
commit
4adcb388e6
  1. 36
      README.md
  2. 53
      examples/multichat/chan.html
  3. BIN
      examples/multichat/demo.gif
  4. 34
      examples/multichat/index.html
  5. 32
      examples/multichat/main.go
  6. 10
      melody.go
  7. 5
      session.go

36
README.md

@ -1,17 +1,15 @@
# melody
# melody [![Build Status](https://travis-ci.org/olahol/melody.svg)](https://travis-ci.org/olahol/melody) [![GoDoc](https://godoc.org/github.com/olahol/melody?status.svg)](https://godoc.org/github.com/olahol/melody)
[![GoDoc](https://godoc.org/github.com/olahol/melody?status.svg)](https://godoc.org/github.com/olahol/melody)
[![Build Status](https://travis-ci.org/olahol/melody.svg)](https://travis-ci.org/olahol/melody)
> :notes: Simple websocket framework for Go
> :notes: Minimalist websocket framework for Go.
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] Built-in ping/pong handling.
* [x] Message buffer for connections making concurrent writing easy.
* [x] Simple broadcasting to all or selected sessions.
* [x] Clear and easy interface similar to `net/http` or Gin.
* [x] A simple way to broadcast to all or selected connected sessions.
* [x] Message buffers making concurrent writing safe.
* [x] Automatic handling of ping/pong and session timeouts.
## Install
@ -21,10 +19,10 @@ go get github.com/olahol/melody
## [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.
[![Chat demo](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/chat)
[![Chat demo](https://cdn.rawgit.com/olahol/melody/master/examples/chat/demo.gif "Demo")](https://github.com/olahol/melody/tree/master/examples/multichat)
```go
package main
@ -43,16 +41,24 @@ func main() {
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.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")
}
```
### [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)

53
examples/multichat/chan.html

@ -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>

BIN
examples/multichat/demo.gif

After

Width: 1504  |  Height: 644  |  Size: 149 KiB

34
examples/multichat/index.html

@ -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>

32
examples/multichat/main.go

@ -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")
}

10
melody.go

@ -63,14 +63,14 @@ func (m *Melody) HandleError(fn func(*Session, error)) {
}
// 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)
if err != nil {
return err
return
}
session := newSession(m.Config, conn)
session := newSession(m.Config, conn, r)
m.hub.register <- session
@ -83,8 +83,6 @@ func (m *Melody) HandleRequest(w http.ResponseWriter, r *http.Request) error {
m.hub.unregister <- session
go m.disconnectHandler(session)
return nil
}
// 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.
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}
m.hub.broadcast <- message
}

5
session.go

@ -2,18 +2,21 @@ package melody
import (
"github.com/gorilla/websocket"
"net/http"
"time"
)
// A melody session.
type Session struct {
Request *http.Request
conn *websocket.Conn
output chan *envelope
config *Config
}
func newSession(config *Config, conn *websocket.Conn) *Session {
func newSession(config *Config, conn *websocket.Conn, req *http.Request) *Session {
return &Session{
Request: req,
conn: conn,
output: make(chan *envelope, config.MessageBufferSize),
config: config,

Loading…
Cancel
Save