/
websocket-irc-example-1
executable file
·223 lines (182 loc) · 6.9 KB
/
websocket-irc-example-1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/perl
# Copyright (c) 2010, David Davis - http://xant.us/
# WebSocket Example 2 - irc client
# Powered by Mojo - http://mojolicious.org/ - http://github.com/kraih/mojo
# and web-socket-js - http://github.com/gimite/web-socket-js
use FindBin;
die "You need to run 'git submodule update --init' to fetch the example requirements\n"
unless -d "$FindBin::Bin/../mojo/lib";
use lib "$FindBin::Bin/../mojo/lib";
use Mojo::IOLoop;
use Mojolicious::Lite;
use strict;
use warnings;
use bytes;
# The loop
my $loop = Mojo::IOLoop->singleton;
# Connection list
my $c = {};
@ARGV = qw( daemon ) unless @ARGV;
my $irc_server = 'irc.perl.org';
my $irc_port = 6667;
websocket '/' => sub {
my $client = shift;
app->log->debug( "client connected, connecting to $irc_server" );
$client->send_message( "Connecting to $irc_server" );
my $rand = int rand( 10000 );
my $con = $c->{"$client"} = {
buffer => "NICK tester$rand\nUSER mojo mojo-websocket 127.0.0.1 :mojo-websocket user\n"
};
$client->on_finish(sub {
app->log->debug( "client finished, dropping conneciton to irc server" );
$loop->drop( $con->{irc} )
if $con->{irc};
delete $c->{"$client"};
return;
});
$client->on_message(sub {
$con->{buffer} ||= '';
$con->{buffer} .= $_[1]; # chunk
$loop->write( $con->{irc} => delete $con->{buffer} )
if $con->{irc} && length $con->{buffer};
});
$con->{_irc} = $loop->connect(
address => $irc_server,
port => $irc_port,
on_connect => sub {
app->log->debug( "Connected to $irc_server" );
$client->send_message( "Connected to $irc_server" );
$con->{irc} = delete $con->{_irc};
$loop->write( $con->{irc} => delete $con->{buffer} )
if length $con->{buffer};
},
on_read => sub {
$client->send_message( $_[2] ); # chunk
},
on_error => sub {
app->log->debug( "Disconnected from $irc_server (connection error)" );
$loop->drop( $client->tx->connection );
},
on_close => sub {
app->log->debug( "Disconnected from $irc_server (hangup)" );
$loop->drop( $client->tx->connection );
}
);
$loop->connection_timeout( $con->{_irc} => 600 );
return;
};
get '/' => 'index';
# see script/flash-policy-server
print "Chrome users: connect to 127.0.0.1 instead of localhost\n";
print "Remember, you need to also run script/flash-policy-server as root for this to work on non WebSocket browsers...\n";
app->start;
1;
__DATA__
@@ index.html.ep
% my $url = $self->req->url->to_abs->scheme( $self->req->is_secure ? 'wss' : 'ws' )->path( '/' );
<!doctype html>
<html>
<head>
<title>Mojo Websocket Demo - Simple IRC Client</title>
<script type="text/javascript">
// only load the flash fallback when needed
if ( !( 'WebSocket' in window ) ) {
document.write([
'<scr'+'ipt type="text/javascript" src="web-socket-js/swfobject.js"></scr'+'ipt>',
'<scr'+'ipt type="text/javascript" src="web-socket-js/FABridge.js"></scr'+'ipt>',
'<scr'+'ipt type="text/javascript" src="web-socket-js/web_socket.js"></scr'+'ipt>'
].join(''));
}
</script>
<script type="text/javascript">
if ( WebSocket.__initialize ) {
// Set URL of your WebSocketMain.swf here:
WebSocket.__swfLocation = 'web-socket-js/WebSocketMain.swf';
}
// example derived from web-socket-js/sample.html
var ws, input, log, btn, timer;
var ch = '#test';
function wsconnect() {
btn.innerHTML = 'Disconnect';
output( 'websocket connecting...\n' );
ws = new WebSocket( '<%= $url %>' );
ws.onopen = function() {
output( 'websocket connected\n' );
ws.send( 'JOIN ' + ch +'\n' );
output( 'send: JOIN ' + ch + '\n' );
};
ws.onmessage = function(e) {
if ( e.data.match( /^PING (\S*)/i ) ) {
ws.send( 'PONG ' + RegExp.$1 + '\n' );
output( '*PING*PONG*\n' );
} else {
output( e.data );
}
};
ws.onclose = function() {
btn.innerHTML = 'Connect';
output( 'websocket disconnected\n' );
};
if ( timer )
clearTimeout( timer );
timer = setInterval( ping, 100000 );
}
function ping() {
// just to keep the websocket alive
if ( ws && ws.readyState == WebSocket.OPEN )
ws.send( '' );
}
function onSubmit() {
if ( input.value == '' )
return;
if ( input.value.match( /^\/nick (.*)/i ) ) {
ws.send( 'nick ' + RegExp.$1 + '\n' );
} else if ( input.value.match( /^\/quote (.*)/i ) ) {
ws.send( RegExp.$1 + '\n' );
output( 'raw send: ' + RegExp.$1 + '\n' );
} else if ( input.value.match( /^\/quit(.*)/i ) ) {
ws.send( 'quit :' + (RegExp.$1 || 'Shagadelic') + '\n' );
} else if ( input.value.match( /^\/join (.*)/i ) ) {
ws.send( 'part ' + ch + '\n' );
output( 'send: part ' + ch + '\n' );
ws.send( 'join ' + RegExp.$1 + '\n' );
output( 'send: join ' + RegExp.$1 + '\n' );
ch = RegExp.$1;
} else {
ws.send( 'privmsg ' + ch + ' :' + input.value + '\n' );
output( 'send: ' + input.value + '\n' );
}
input.value = '';
try{ input.focus(); } catch(e) { };
}
function onCloseClick() {
if ( btn.innerHTML == 'Disconnect' )
ws.close();
else
wsconnect();
}
function output(str) {
var escaped = str.replace( /&/, '&', 'g' ).replace( /</, '<', 'g' ).
replace( />/, '>', 'g' ).replace( /"/, '"', 'g' );
log.innerHTML = log.innerHTML + escaped;
log.scrollTop = log.scrollHeight;
}
function init() {
input = document.getElementById( 'input' );
log = document.getElementById( 'log' );
btn = document.getElementById( 'btn' );
wsconnect();
}
window.onload = init;
</script>
</head>
<body>
<h2>Mojo WebSocket Demo - Simple IRC Client</h2>
<pre id="log" style="height:400px; width: 99%; overflow: auto;"></pre>
<form onsubmit="onSubmit(); return false;">
<input type="text" id="input" style="width:400px">
<input type="submit" value="Send">
<button id="btn" onclick="onCloseClick(); return false;">Connect</button>
</form>
</body>
</html>