Skip to content

Instantly share code, notes, and snippets.

@aminamid
Created April 17, 2014 03:28
Show Gist options
  • Save aminamid/10950911 to your computer and use it in GitHub Desktop.
Save aminamid/10950911 to your computer and use it in GitHub Desktop.
Chatserver by angularJS + geventwebsocket + flask

Location

./test.py ./static/index.html ./static/client.js

Usage

python ./test.py

var app = angular.module('app', []);
app.factory('ChatService', function() {
var service = {};
service.connect = function() {
if(service.ws) { return; }
var ws = new WebSocket("ws://remote.host.com:8080/socket/");
ws.onopen = function() {
service.callback("Succeeded to open a connection");
};
ws.onerror = function() {
service.callback("Failed to open a connection");
}
ws.onmessage = function(message) {
service.callback(message.data);
};
service.ws = ws;
}
service.send = function(message) {
service.ws.send(message);
}
service.subscribe = function(callback) {
service.callback = callback;
}
return service;
});
function AppCtrl($scope, ChatService) {
$scope.messages = [];
ChatService.subscribe(function(message) {
$scope.messages.push(message);
$scope.$apply();
});
$scope.connect = function() {
ChatService.connect();
}
$scope.send = function() {
ChatService.send($scope.text);
$scope.text = "";
}
}
<html ng-app="app">
<head>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-resource.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.1/angular-cookies.min.js"></script>
<script src="/static/client.js"></script>
<style>
body { margin-top: 10px; }
input.message { height: 30px; }
</style>
</head>
<body ng-controller="AppCtrl">
<form class="form-inline">
<button ng-click="connect()" class="btn">Connect</button>
<input type="text" ng-model="text" placeholder="input message to send" class="message"></input>
<button ng-click="send()" class="btn">send</button>
</form>
<table class="table table-striped">
<tr ng-repeat="message in messages track by $index">
<td>{{message}}</td>
</tr>
</table>
</body>
</html>
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pprint
from geventwebsocket.handler import WebSocketHandler
from geventwebsocket import WebSocketServer,WebSocketApplication,Resource
from gevent.pywsgi import WSGIServer
from flask import Flask, request, url_for
from werkzeug.exceptions import abort
from werkzeug.debug import DebuggedApplication
pp = pprint.PrettyPrinter(indent=2)
flask_app = Flask(__name__)
flask_app.debug = True
class ChatApplication(WebSocketApplication):
connections ={}
index=1
@classmethod
def get_index(cls):
return cls.index
@classmethod
def countup_index(cls):
cls.index +=1
@classmethod
def get_connections(cls):
return cls.connections
@classmethod
def add_connections(cls,key,value):
cls.connections[key] = value
@classmethod
def remove_connections(cls,key):
del(cls.connections[key])
def on_open(self):
name = "Guest{0}".format(self.get_index())
self.add_connections(self.ws.handler.active_client,name)
print "active_client={0}".format(self.ws.handler.active_client)
print "name={0}".format(name)
self.countup_index()
for client in self.ws.handler.server.clients.values():
client.ws.send("[Server] Hello {0}".format(name))
client.ws.send("[Server] Members are: {0}".format(self.login_names()))
def on_close(self,reason):
self.remove_connections(self.ws.handler.active_client)
def on_message(self,msg):
sender = self.get_connections()[self.ws.handler.active_client]
self.broadcast(sender,msg)
def login_names(self):
return ", ".join(self.connections.values())
def broadcast(self, sender,message):
for client in self.ws.handler.server.clients.values():
client.ws.send("[{0}] {1}".format(sender,message))
@flask_app.route('/')
def index():
return flask_app.send_static_file('index.html')
@flask_app.route('/static/client.js')
def clientjs():
return flask_app.send_static_file('client.js')
if __name__ == '__main__':
WebSocketServer(
('', 8080),
Resource({
'^/socket': ChatApplication,
'^/.*': DebuggedApplication(flask_app)
}),
debug=True
).serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment