Categorygithub.com/lonnng/starx
modulepackage
0.0.0-20170807025737-bc5efd8bc7ac
Repository: https://github.com/lonnng/starx.git
Documentation: pkg.go.dev

# README

DEPRECATED

Do not use in a production environment, new project ref: https://github.com/lonnng/nano/

~~~Starx -- golang based game server framework~~~

Inspired by Pomelo, rewrite with golang.

Client Library

Chat Room Demo

implement a chat room in 100 lines with golang and websocket starx-chat-demo

  • server

    package main
    
    import (
    	"github.com/lonnng/starx"
    	"github.com/lonnng/starx/component"
    	"github.com/lonnng/starx/serialize/json"
    	"github.com/lonnng/starx/session"
    )
    
    type Room struct {
    	component.Base
    	channel *starx.Channel
    }
    
    type UserMessage struct {
    	Name    string `json:"name"`
    	Content string `json:"content"`
    }
    
    type JoinResponse struct {
    	Code   int    `json:"code"`
    	Result string `json:"result"`
    }
    
    func NewRoom() *Room {
    	return &Room{
    		channel: starx.ChannelService.NewChannel("room"),
    	}
    }
    
    func (r *Room) Join(s *session.Session, msg []byte) error {
    	s.Bind(s.ID)     // binding session uid
    	r.channel.Add(s) // add session to channel
    	return s.Response(JoinResponse{Result: "sucess"})
    }
    
    func (r *Room) Message(s *session.Session, msg *UserMessage) error {
    	return r.channel.Broadcast("onMessage", msg)
    }
    
    func main() {
    	starx.SetAppConfig("configs/app.json")
        starx.SetServersConfig("configs/servers.json")
    	starx.Register(NewRoom())
    
    	starx.SetServerID("demo-server-1")
    	starx.SetSerializer(json.NewJsonSerializer())
    	starx.Run()
    }
    
    
  • client

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Chat Demo</title>
    </head>
    <body>
    <div id="container">
        <ul>
            <li v-for="msg in messages">[<span style="color:red;">{{msg.name}}</span>]{{msg.content}}</li>
        </ul>
        <div class="controls">
            <input type="text" v-model="nickname">
            <input type="text" v-model="inputMessage">
            <input type="button" v-on:click="sendMessage" value="Send">
        </div>
    </div>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/vue/1.0.26/vue.min.js" type="text/javascript"></script>
    <!--[starx websocket library](https://github.com/lonnng/starx-client-websocket)-->
    <script src="protocol.js" type="text/javascript"></script>
    <script src="starx-wsclient.js" type="text/javascript"></script>
    <script>
        var v = new Vue({
            el: "#container",
            data: {
                nickname:'guest' + Date.now(),
                inputMessage:'',
                messages: []
            },
            methods: {
                sendMessage: function () {
                    starx.notify('Room.Message', {name: this.nickname, content: this.inputMessage});
                    this.inputMessage = '';
                }
            }
        });
    
        var onMessage = function (msg) {
            v.messages.push(msg)
        };
    
        var join = function (data) {
            if(data.code == 0) {
                v.messages.push({name:'system', content:data.result});
                starx.on('onMessage', onMessage)
            }
        };
    
        starx.init({host: '127.0.0.1', port: 3250}, function () {
            starx.request("Room.Join", {}, join);
        })
    </script>
    </body>
    </html>
    

Demo

Wiki

Homepage

The MIT License

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

EnableCluster enable cluster mode.
No description provided by the author
Callback when session closed Waring: session has closed,.
No description provided by the author
Run server.
Set special server initial function, starx.Set("oneServerType | anotherServerType", func(){}).
SetCheckOriginFunc set the function that check `Origin` in http headers.
Set heartbeat time internal.
SetMasterServerID set master server id, config must be contained in servers.json master server id must be set when cluster mode enabled.
No description provided by the author
Customize serializer.
No description provided by the author
Set the path of servers.json.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

No description provided by the author
Group represents a session group which used to manage a number of sessions, data send to the group will send to all session in it.

# Type aliases

No description provided by the author
No description provided by the author