Ben Piramit ve socket.io çerçeveler kullanarak basit WebSocket uygulama oluşturmak çalışıyorum. Sunucu tarafı kodu:Websockets, piramit ve socket.io ile nasıl kullanılır?
from pyramid.response import Response
from pyramid_socketio.io import SocketIOContext, socketio_manage
import gevent
def includeme(config):
'''
This method is called on the application startup.
'''
config.add_route('socket.io', 'socket.io/*remaining')
class ConnectIOContext(SocketIOContext):
# self.io is the Socket.IO socket
# self.request is the request
def msg_connect(self, msg):
print "Connect message received", msg
self.msg("connected", hello="world")
# Socket.IO implementation
@view_config(route_name="socket.io")
def socketio_service(request):
print "Socket.IO request running"
print request
retval = socketio_manage(ConnectIOContext(request))
return Response(retval)
Müşteri kodu:
<script>
var socket = null;
$(document).ready(function() {
socket = new io.Socket(null, null);
socket.on('connect', function() {
console.log("Connected");
socket.send({type: "connect", userid: 123});
});
socket.on('message', function(obj) {
console.log("Message received");
console.log("Message", JSON.stringify(obj));
if (obj.type == "some") {
console.log("do some");
}
});
socket.on('error', function(obj) {
console.log("Error", JSON.stringify(obj));
});
socket.on('disconnect', function() {
console.log("Disconnected");
});
console.log("Connecting...");
socket.connect();
});
</script>
Ben bağlantı için web prizler kullanmak için bu kodu gerekir, ancak XHR-yoklama geri düşer. Nasıl düzeltebilirim? Ivan, önceden
teşekkürler.
Soruyu gerçekten yanıtlamak için, istemci kütüphanesinin 0,6 sürümünü kullanan io.Socket() çağrısının ikinci parametresi olarak aktarılan bir {transports: ["websocket"]} nesnesi olmalıdır. JS lib'in 0.7+ sürümünü kullanıyorsanız, sunucu tarafında ayarlanmamıştır. Web-soketleri etkinleştirilmiş bir web tarayıcısı kullanmalısınız (Chrome, çalıştığını biliyorum). – abourget
gevent-socketio gelişimi oldukça ölü, çok üzerinde güvenmeyin. –
Ravi- yerine kullanmanızı tavsiye ne gevent-socketio kullanarak bu sırt bahsedildiğinde Hem cevaplar? – Amoliski