我正在尝试构建通知消息系统.我使用SimpleWsServer.php服务器示例.我想在服务器上完成任务时将通知推送到用户的浏览器.这需要使用PHP完成,我无法找到显示它的教程.当PHP服务器作为管理器运行时,所有教程似乎都显示了发送和接收的tavendo/AutobahnJS脚本.
是否可以使用php脚本向订阅者发送消息?
我有一个服务器,使用Twisted和Autobahn侦听端口80上的WebSocket连接.我想让它也提供静态HTML页面,因为当客户端不想使用WebSocket时.使用Twisted和Autobahn可以同时做两件事吗?
我没有为我的问题找到任何解决方案.我需要创建一个带有两个线程的python应用程序,每个线程使用autobahn库连接到WAMP路由器.
跟着我写下我的实验代码:
wampAddress = 'ws://172.17.3.139:8181/ws'
wampRealm = 's4t'
from threading import Thread
from autobahn.twisted.wamp import ApplicationRunner
from autobahn.twisted.wamp import ApplicationSession
from twisted.internet.defer import inlineCallbacks
class AutobahnMRS(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print("Sessio attached [Connect to WAMP Router]")
def onMessage(*args):
print args
try:
yield self.subscribe(onMessage, 'test')
print ("Subscribed to topic: test")
except Exception as e:
print("Exception:" +e)
class AutobahnIM(ApplicationSession):
@inlineCallbacks
def onJoin(self, details):
print("Sessio attached [Connect to WAMP Router]")
try:
yield self.publish('test','YOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO')
print ("Subscribed to topic: test")
except Exception as e:
print("Exception:" …
Run Code Online (Sandbox Code Playgroud) Poloniex不会将每条消息都返回给我的套接字.我使用以下代码阅读消息,有时我会收到连续的消息号,但有时会丢失10条消息:
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner
from asyncio import coroutine
class PoloniexComponent(ApplicationSession):
def onConnect(self):
self.join(self.config.realm)
@coroutine
def onJoin(self, details):
def onTrollbox(*args):
print("type: ", args[0])
print("message_number: ", args[1])
print("user_name: ", args[2])
print("message: ", args[3])
print("reputation: ", args[4])
try:
yield from self.subscribe(onTrollbox, 'trollbox')
except Exception as e:
print("Could not subscribe to topic:", e)
runner = ApplicationRunner("wss://api.poloniex.com", "realm1")
runner.run(PoloniexComponent)
Run Code Online (Sandbox Code Playgroud)
有人知道更好的解决方案吗?我试过这个,但它根本不起作用:
from websocket import create_connection
ws = create_connection("wss://api.poloniex.com")
ws.send("trollbox")
result = ws.recv()
print "Received '%s'" % result
ws.close()
Run Code Online (Sandbox Code Playgroud) 我有一个 Crossbar.js 实现,其中数据通过 Crossbar websocket 服务器在客户端(网站)和服务器(Node.js)之间发送。双方使用 Autobahn.JS 连接到 Crossbar 服务器。
连接工作正常,但似乎客户端和服务器都在随机时刻断开并重新连接。这种情况大约每两分钟发生一次。我还看到双方的连接断开不是同时发生的。这让我认为问题出在我在双方使用的高速公路实现上(客户端和服务器大致相同)。
下面是我用来从 Node.js 连接到 Crossbar 服务器的方法。浏览器的版本几乎相同。我只更改了订阅并将const
和let
变量更改为var
.
start(connectionConfig) {
const self = this;
self.host = connectionConfig.host;
self.realm = connectionConfig.realm;
self.channelPrefix = connectionConfig.channelPrefix;
try {
// Start an Autobahn websocket connection
self.connection = new autobahn.Connection({"url": self.host, "realm": self.realm});
self.connection.onopen = function(session) {
// Save session in class
self.session = session;
// Listen for incoming commands
session.subscribe(self.channelPrefix + 'smdc-server', self.onCommand.bind(self));
// Copy the class variable buffer to …
Run Code Online (Sandbox Code Playgroud) 我刚开始使用Crossbar.io来实现实时统计页面.我看了很多代码示例,但我无法弄清楚如何执行此操作:
我有一个Django的服务(为避免混淆,你可以假设I'm谈论views.py功能),我想它在一个特定的主题发布消息,每当它被调用.我见过这些方法:(1)扩展ApplicationSession和(2)使用"runned"的Application实例.
它们都不适合我,因为Django服务不在一个类中,也不是作为一个独立的python文件执行,所以我找不到一种方法来调用"发布"方法(即我唯一想在服务器端做的事情).
我试图让"StatsBackend",延伸ApplicationSession的实例,并发布一些信息,但StatsBackend._instance是无总是(甚至当我执行"横梁开始"和StatsBackend.INIT()被调用).
StatsBackend.py:
from twisted.internet.defer import inlineCallbacks
from autobahn import wamp
from autobahn.twisted.wamp import ApplicationSession
class StatsBackend(ApplicationSession):
_instance = None
def __init__(self, config):
ApplicationSession.__init__(self, config)
StatsBackend._instance = self
@classmethod
def update_stats(cls, amount):
if cls._instance:
cls._instance.publish('com.xxx.statsupdate', {'amount': amount})
@inlineCallbacks
def onJoin(self, details):
res = yield self.register(self)
print("CampaignStatsBackend: {} procedures registered!".format(len(res)))
Run Code Online (Sandbox Code Playgroud)
test.py:
import StatsBackend
StatsBackend.update_stats(100) #Doesn't do anything, StatsBackend._instance is None
Run Code Online (Sandbox Code Playgroud) 所以我有这个小方案项目,它使用来自高速公路扭曲的python中的websockets.客户端和服务器之间的连接以及数据传输工作完美,但在TCP连接关闭后我无法正常退出程序.她是代码:
class MyClientProtocol(WebSocketClientProtocol):
def onConnect(self, response):
print("Server connected: {0}".format(response.peer))
def onOpen(self):
print("WebSocket connection open.")
def hello():
from twisted.internet import reactor
self.sendMessage(u"Hello, world!".encode('utf8'))
self.sendMessage(b"\x00\x01\x03\x04", isBinary=True)
#self.factory.reactor.callLater(1, hello)
#self.reactor.callFromThread(reactor.stop)
#reactor.callFromThread(reactor.stop)
#self.factory.reactor.callFromThread(reactor.stop)
# start sending messages every second ..
hello()
return
def onMessage(self, payload, isBinary):
if isBinary:
print("Binary message received: {0} bytes".format(len(payload)))
else:
print("Text message received: {0}".format(payload.decode('utf8')))
def onClose(self, wasClean, code, reason):
print("WebSocket connection closed: {0}".format(reason))
def WebCon():
import sys
from twisted.python import log
from twisted.internet import reactor
log.startLogging(sys.stdout)
factory = WebSocketClientFactory("ws://localhost:8080", debug=False) …
Run Code Online (Sandbox Code Playgroud) 注意:我将Autobahn.js用于客户端WAMP实现,而when.js用于promises.
我正在尝试创建可重用的代码,以便只存在一个websocket'session'或连接,并且只要dev想要使用autobahn订阅主题,他们就可以使用当前连接对象来执行此操作存在; 否则会创建一个新的.
我的问题是,如果连接已经存在,我必须使用a setTimeout()
等待一秒钟以确保它实际连接,然后复制所有订阅代码 - 我根本不喜欢这个.
这是我目前的代码:
(function() {
var connection = null;
subscribeTo('subject', __userId, __token, function(onconnect) {
console.log('Yay, connected');
});
function subscribeTo(subject, userId, token, onConnect, onDisconnect) {
if (connection === null)
{
connection = new ab.Session('ws://localhost:8080', function(onopen) {
connection.subscribe(JSON.stringify({subject: subject, userId: userId, token: token}), function(subscription, data) {
data = $.parseJSON(data);
// Do something with the data ...
});
if (typeof onConnect === 'function') {
onConnect();
}
}, function(onclose) {
if (typeof onDisconnect …
Run Code Online (Sandbox Code Playgroud) 我需要编写一个WebSocket服务器,并且通过阅读购买的书籍来学习Node JS。该服务器用于非常快速的游戏,因此我需要尽快将小消息流式传输到客户端组。
之间有什么区别?
高速公路| JS:http://autobahn.ws/js/
和
埃纳罗斯(Einaros):https : //github.com/einaros/ws
?
我听说Autobahn非常强大,能够在没有负载均衡器的情况下处理20万个客户端,因此我想知道是否有更多经验的人可以建议我选择一个或另一个库是否有任何优势。
我尝试通过 Thruway 设置一个可以管理多个组的 websocket 服务器。类似于聊天应用程序,每个客户端可以同时订阅一个或多个,并将消息广播到整个聊天室。我设法用一个古老版本的 Ratchet 做到了这一点,但由于它运行得不是很流畅,我想切换到高速公路。可悲的是,我找不到任何可以管理群组的东西。到目前为止,我作为 websocket-manager 有以下内容,并且客户端正在使用 Autobahn|js (18.x) 的当前版本。
有没有人知道是否可以使用以下内容管理订阅组?
<?php
require_once __DIR__.'/../vendor/autoload.php';
use Thruway\Peer\Router;
use Thruway\Transport\RatchetTransportProvider;
$router = new Router();
$router->addTransportProvider(new RatchetTransportProvider("0.0.0.0", 9090));
$router->start();
Run Code Online (Sandbox Code Playgroud) 我有这个服务器
我想使用以下代码连接到服务器:
ws = create_connection("wss://127.0.0.1:9000")
Run Code Online (Sandbox Code Playgroud)
我需要添加哪些选项create_connection
?添加sslopt={"cert_reqs": ssl.CERT_NONE}
不起作用:
websocket._exceptions.WebSocketBadStatusException: Handshake status 400
Run Code Online (Sandbox Code Playgroud)