Desktop Notication火狐扩展使用Websocket方法连接服务器求教

旧城的骁珖 发布于 2015/10/09 14:30
阅读 375
收藏 0
这是perl测试代码 无法建立正常的webscoket链接
use Mojo::UserAgent;
use Data::Dumper;
use feature qw(say);
my $ua = Mojo::UserAgent->new;
$ua->transactor->name('Mozilla/5.0 (X11; Linux x86_64; rv:41.0) Gecko/20100101 Firefox/41.0');
$ua->websocket('ws://desktopnotifications.hci.simtech.uni-stuttgart.de:10935/' =>  ['v1.proto'] => sub {
  my ($ua, $tx) = @_;
  say 'WebSocket handshake failed!' and return unless $tx->is_websocket;
  say 'WebSocket handshake success!';
  $tx->on(finish => sub {
    my ($tx, $code, $reason) = @_;
    say "WebSocket closed with status $code.";
  });
  $tx->on(message => sub {
    my ($tx, $msg) = @_;
    say "WebSocket message: $msg";
    $tx->finish;
  });
  $tx->send('Hi!');
});
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;


 

 
下边是官方代码
var debug  = false;
var audio  = new Audio("sounds/new.ogg");
var socket = null;
var code   = null;
var sound  = null;
var init   = false;
var sd     = 2000; // 2s
var sl     = 0;

function log(str) {
	if(debug) console.log("[ADN] " + str);
}

// Addon
// ------------------------------------------------------------------------------

self.port.on("setup", function() {
	if(!init && code != null && code != "") {
		setup();
	} else {
		log("setup event fired");
	}
});

self.port.on("setcode", function(msg) {
	log("setcode");
	code = msg;

	if(!init && code != null && code != "")
		setup();

	socketSubscribe(code);
});

self.port.on("setsound", function(enabled) {
	log("setsound");
	if(sound != null && enabled) audio.play();
	sound = enabled;
});

// Socket
// ------------------------------------------------------------------------------

function setup() {

	log("setup");

	socket = io("wss://desktopnotifications.hci.simtech.uni-stuttgart.de:10935/", {
		"reconnectionDelay": 20000,
		"reconnectionDelayMax": 120000,
		"forceNew": true
	});

	socket.on("notification", function(data) {
		if(data) {
			var json = JSON.parse(data);
			log(json);
			if(json == null) return;
			self.port.emit("notification", json);
			if(sound && Date.now() > sl + sd) {
				sl = Date.now();
				audio.play();
			}
		}
	});

	socket.on("remove", function(data) {
		if(data) {
			var json = JSON.parse(data);
			log(json);
			if(json == null) return;
			self.port.emit("remove", json);
		}
	});

	socket.on("connect", function() {
		log("connect");
		if(debug) self.port.emit("notification", {title: 'DEBUG', content: 'Connected', package: 'org.hcilab.projects.notification.debug.connect'});
		socketSubscribe(code);
	});

	socket.on("reconnect", function() {
		log("reconnect");
		if(debug) self.port.emit("notification", {title: 'DEBUG', content: 'Reconnected', package: 'org.hcilab.projects.notification.debug.reconnect'});
		socketSubscribe(code);
	});

	socket.on("reconnect_attempt", function() {
		log("reconnect_attempt");
		if(debug) self.port.emit("notification", {title: 'DEBUG', content: 'Reconnect Attempt', package: 'org.hcilab.projects.notification.debug.reconnect.attempt'});
	});

	socket.on("disconnect", function() {
		log("disconnect");
		if(debug) self.port.emit("notification", {title: 'DEBUG', content: 'Disconnected', package: 'org.hcilab.projects.notification.debug.disconnect'});
	});

	socket.on("error", function() {
		log("error");
		if(debug) self.port.emit("notification", {title: 'DEBUG', content: 'Error', package: 'org.hcilab.projects.notification.debug.error'});
	});

	init = true;

}

function socketSubscribe(code) {
	if(socket != null)
		socket.emit("subscribe", code);
}
加载中
0
旧城的骁珖
旧城的骁珖
我擦 看源码但是不理解每一步果然不靠谱。终于解决了
返回顶部
顶部