VideoChat Plugin Development

Since this is a videochat plugin, the core part is of course imeplementing video chatting between users. It's really simple actually.

If you have heard of WebRTC, you should be aware that modern web browsers have great power. I'll make a brief introduction to WebRTC here, but if you want to truely understand it, further searching and reading is necessary.

WebRTC is a free, open project that enables web browsers with Real-Time Communications (RTC) capabilities via simple Javascript APIs.WebRTC allows web pages to access local multimedia devices like a webcam and microphone, and transmit these media streams to another WebRTC capable browser via a peer-to-peer network channel. These media streams can also be accompanied by a powerful data channel that lets developer exchange arbitrary data between two peers! Its mission is to enable rich, high quality, RTC applications to be developed in the browser via simple Javascript APIs and HTML5.

Visit apprtc.appspot.com (one of WebRTC's official examples to show how it works), you'll find that it's just the application I use for my plugin. All I did is putting the website into an <iframe>, and that's it, incrediblely simple but works flawlessly!

View the code for more information.

Though the appspot site works really well, it has some constraints. If two people have entered the same room (same url), then the room gets full for it only supports a peer-to-peer channel. To avoid such mess, everytime a user comes to the VideoChat page, a random number will be generated and act as the query string of apprtc.appspot.com, which prevents you from entering a full or half-full room.

Frankly speaking, although it's a plugin to implement video chatting, the most difficult part is not on videochat, but on how to invite your friends to talk to you.To do that, I use elgg built-in User pickers view.

Next step is to find out how elgg actually sends a message, it's rather easy once you have the experience using Chrome Devtools or firebug.It turns out that a message is an HTTP POST request.

e.g.

There is one thing left which is implementing your own sending/inviting machanism and making a redirection once your friends clicks the link in your msg. I'll let code do the talking:

li += '<button id="invite" float="left" height=10 width=100>invite</button>';  
$('<li>').html(li).appendTo(users);

These two lines add the invite button.

var redirect_url = base_url + "videochat/vc?r=" + rand_num;
var body = '<a href="' + redirect_url + '">请和我签订契约,加入视频聊天吧!</a>';
var msg_params = {
    __elgg_ts:elgg.security.token.__elgg_ts,
    __elgg_token:elgg.security.token.__elgg_token,
    recipient_guid: info.guid,
    subject:'有朋友邀请您参与视频聊天',
    body: body,
};

$('#invite').click(function(){
    $.ajax({
        url: base_url + 'action/messages/send',
        type: 'POST',
        data: msg_params,
        success: function(msg){
            alert('your msg has been sent');
        }
    })
});

This part send the HTTP POST request we mentioned above.Note that the content of an invite message contains a link, redirecting the invitee to the videochat page. The random number is also passed as a query string parameter, forcing the message invitee enter the same room as inviter. The code below shows how it is done.

if ( window.location.search != "" )  // has query string. 
    rand_num = window.location.search.substr(3);  //?r=num
else
    rand_num = Math.floor(Math.random()*900000+100000);
$('#video').prop('src', "https://apprtc.appspot.com/?r=" + rand_num); 

some other things you should pay attention to

  1. I use ajax to prevent current page from reloading after sending a request. Why? Because two people can chat if and only if their iframe elements have the same src attribute (i.e. same room). If you reload, as mentioned before, a new random number will be generated thus making the inviter enter a different room.
  2. When sending HTTP request, __elgg_ts and __elgg_token are necessary parameters. This is a machanism elgg uses to validate message. Luckily these two variables are stored in global scope:
  3. If you try to search friends in another page rather than the videochat Page, you still get a invite button, however when clicked, you will be alerted:

    This is done by detecting the existense of that random number since it only exists in the videochat page.
if (typeof rand_num != "undefined"){
    //add invite button click EventListener, see above code
}
else
    alert('do this on VideoChat page!');

慎用“傲娇”,这个词表意不明确且misleading

这篇日志写起来很容易,决定是否要写却不容易——总感觉这像是一个咬文嚼字的古怪nerd才会写的东西。但既然想写,那肯定就还是要写。

从二次元产生的词汇很多,在大部分情况下,这些词汇仅仅在相对封闭的小部分人群中被使用。然而最近,“傲娇”这个词似乎总能在各种场合看见或是听见。

目前的状况是,我可能在某些场合听说了这个词,然后查了一下或者没查它的意思,接着按照自己的理解为这个词定义了一个意思,最后开始使用它。问题就在于,这个词的语境其实并不是那么简单,并不是随便就可以用的。就所遇到的情况来看,这个词往往被用在了不合适的语境。

自然,某个词汇的使用是无法被控制且不应该被控制的,但使用这个词带来了问题:

站在我的角度,会不知道说话人/作者究竟想表达什么意思。“傲娇”?哪里傲娇了?没有上下文怎么突然就讲出这个词?我知道TA肯定想表达某种意思,但又非常能肯定这个意思并非我所熟悉的那一个。那么究竟是什么意思?不知道。

另一方面,站在大多数人的角度看,如果没有听过这个词,当然也不知道说话人/作者究竟想表达什么。

所以,“傲娇”一词在日常生活中的使用完全是无效率的,它完全无法承担表意的作用。更重要的是,如果按照这个词的真正意义去理解它所在的句子,那么要么得到一个完全无意义的句子,要么得到一个偏离原意十万八千里的意思。误解比无效率更可怕。

所以建议就是:

不要在三次元使用“傲娇”,除非你真正知道你在说什么。

Never use it in real world unless you really know what you are talking about.

因为很重要所以说两遍

这里并不打算探讨词的用法,而且作为一个很有节操的人我也不想用“才、才不是因为你过来的,只是放学顺便路过而已”这样的句子来举例(喂这不就是举例吗!)。总之,不要用就好了。


PS:我一直认为“现充”是一个比“高富帅/白富美”表意准确性和丰富性强100倍的词,虽然它们的含义其实有些许不同,但替代使用是完全可以的。

Life is something you experience

好久没回学校了,这次终于逮着机会。和卓爷一起忽悠了二十分钟,大概最终也没讲明白EE是干嘛的,不过卓爷倒是把微电那边说得比较清晰。后来想到导引课都得上一个学期,给十几分钟也确实勉为其难了。

这趟还是挺有意思的,我总觉得,和那些正在经历那些自己经历过的事情的人聊天,是一件非常有趣的事。

有人非常认真地问我,电子和计算机到底去哪。他似乎是对互联网感兴趣。我跟他说,你要是对硬件不感兴趣就直接不要来电子。不知道他最终怎么选。有个女生,看起来很瘦弱,居然非常想搞跟导弹有关的研究,还一个劲问我国防班的事。我说你没必要去国防班啊,课程都是一样的。可是她好像很执着。最后我明白了,她是想进军队,因为她感觉军队的气氛(不确定是不是这个词反正差不多)很好。毕竟我父母都在军队干活,我跟她讲了点实际情况,说得非常直接,大体上是劝她慎重考虑。最终是否说服了她?看样子,多半是没有。

有一个孩子缠着问了好久各个专业的情况,后来他才说自己是高二的。。。我靠高二你急个啥啊。不过问问专业的情况总归是有好处。他是想去搞航空航天,问了一大堆航院的状况。结果我说不出个所以然,还是只能给他说说电子系的情况。聊得太多了,只记得他问我要做出电路板,是不是需要很强的形象思维和画图能力。当听到“完全不需要,那些都是计算机画的”的时候,不知道是否颠覆了他的世界观。最后实在没辙,好在他也终于跑去问杜南基科的情况,我才得以解围。

看到有个女孩问卓爷问题,好像是什么东西学不太懂。我问是啥学不懂?她说单片机编程学不懂。。。我当时就有一种撞墙的冲动。很自然地就问怎么想到去学单片机,她说,是学校的选修课。虽然我高中也上过选修但真没想到RDFZ已经逆天到这种程度了。好吧,单片机我也算是玩过,就又详细问了问。一问终于明白了,原来既没学过C也没学过数电(喂这不是很自然的嘛)。卓爷说你要是学了C和数电,单片机编程自然就简单了,现在对你确实有些难度。结果她说程序是能看懂的,就是写不出来。于是我和卓爷只能一脸黑线地安慰她,说,你在高中,懂这么多已经很厉害了,不用着急。

后来想想,遥控校门的那个装置大概就是单片机选修课上的作品吧。

随后听说还开了数据库的选修课。。。再次刷新了我的三观。

有个学弟过来对我说,学长,我已经保送电子系了。我说,你搞物理竞赛的吧。果然没错。年年皆如此。

在走廊里偶然碰见张 @日娜 ,她很吃惊地问我怎么在这。于是我便知道这活动的宣传状况了。老师急着走,聊了几句就说再见了。

之后碰见吴重为,当时我几乎不敢相信自己的眼睛——这家伙四年一丁点变化都没有,怎么说也太奇怪了。大概人间总有各种特例,吴重为的存在已经是一个特例,老天大概不介意为他再破例一次。

吴重为说话还是那么逗,正好我也讲完了,就听他跟学弟聊天,权当听单口相声。

然后他莫名其妙地就和一对家长聊上了,仔细一看,孩子也在。原来这孩子已经保送基科了。无疑,基科3字班马上就要迎来女神,拥有CMO银牌的女神。家长说曾经还想过让孩子学医,吴重为说这脑子可可可不能浪费了,必必必须要为科学作出贡献才行。

大概也就是这样了。回到学校,看到孩子们,让我又一次感受到青春的活力,在大学呆久了,似乎好久没接触到这种活力,以后估计也更不可能了。他/她们有的迷茫,有的,则是没来由地坚定,像极了以前的我们。说真的,我不认为二十分钟的专业介绍能给他们带来什么实质的帮助,大学终究是要自己体验的,往后的生活,那些我也未曾活过的生活,也是如此。我跟那对家长说,大学其实仍然是一个探寻自己真正想法从而真正认识自我的过程。如果只让我讲一句话,那就是这句。

以囧叔在母校演讲时的一句话作为结尾吧:

College is something you complete, Life is something you experience.


top