Skip to content

Commit

Permalink
add live chat window , resolved #1
Browse files Browse the repository at this point in the history
  • Loading branch information
typcn committed May 18, 2015
1 parent 6ebb171 commit 9d914a6
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 15 deletions.
28 changes: 20 additions & 8 deletions bilibili/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
</connections>
</button>
<button verticalHuggingPriority="750" id="nUV-At-1HF">
<rect key="frame" x="90" y="106" width="82" height="32"/>
<rect key="frame" x="67" y="105" width="82" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="选择弹幕" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Wtj-ZP-diZ">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand All @@ -151,7 +151,7 @@
</connections>
</button>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="LX8-zA-OlO">
<rect key="frame" x="188" y="159" width="172" height="17"/>
<rect key="frame" x="165" y="158" width="172" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="允许所有 ffmpeg 支持的格式" id="9eb-5r-SWr">
<font key="font" metaFont="system"/>
Expand All @@ -160,16 +160,16 @@
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" id="EP3-TH-MpO">
<rect key="frame" x="188" y="115" width="199" height="17"/>
<rect key="frame" x="165" y="114" width="274" height="17"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="支持 Bilibili Acfun Niconico 格式" id="E2h-KF-1Hp">
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="由于弹幕引擎重写,暂时只支持 Bilibili 格式" id="E2h-KF-1Hp">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button verticalHuggingPriority="750" id="MRM-cn-yyW">
<rect key="frame" x="90" y="148" width="82" height="32"/>
<rect key="frame" x="67" y="147" width="82" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="选择视频" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="VnN-7Y-cRN">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
Expand Down Expand Up @@ -577,7 +577,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES"/>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" animates="YES" imageScaling="proportionallyDown" image="loading" id="s2c-ae-PoS"/>
</imageView>
<button hidden="YES" verticalHuggingPriority="750" id="xVW-zA-HWk">
<button hidden="YES" verticalHuggingPriority="750" id="xVW-zA-HWk" userLabel="ShowPostCommentButton">
<rect key="frame" x="7" y="-314" width="14" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Button" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="259-A0-YXE">
Expand All @@ -597,6 +597,17 @@
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<button hidden="YES" verticalHuggingPriority="750" id="AOg-hv-Slo" userLabel="ShowLiveChatButton">
<rect key="frame" x="423" y="116" width="15" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<buttonCell key="cell" type="push" title="Button" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="gP4-5d-Xlk">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<segue destination="ffQ-wz-5FH" kind="show" id="U2J-aP-P9v"/>
</connections>
</button>
</subviews>
</view>
<userDefinedRuntimeAttributes>
Expand All @@ -605,6 +616,7 @@
<connections>
<outlet property="PostCommentButton" destination="xVW-zA-HWk" id="Exg-Nr-pCW"/>
<outlet property="loadingImage" destination="d4M-72-jUI" id="fmB-92-FBV"/>
<outlet property="showLiveChat" destination="AOg-hv-Slo" id="zQr-hq-29W"/>
<outlet property="subtip" destination="nwA-xX-SSg" id="oAy-Dk-ud7"/>
<outlet property="textTip" destination="q3q-RS-pq9" id="aqJ-eF-Wbt"/>
</connections>
Expand All @@ -616,7 +628,7 @@
<!--Window Controller-->
<scene sceneID="PkE-Ql-sEX">
<objects>
<windowController storyboardIdentifier="LiveChatWindow" id="ffQ-wz-5FH" sceneMemberID="viewController">
<windowController storyboardIdentifier="LiveChatWindow" showSeguePresentationStyle="single" id="ffQ-wz-5FH" sceneMemberID="viewController">
<window key="window" title="实时弹幕" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="i8z-Ck-rGI">
<windowStyleMask key="styleMask" titled="YES" closable="YES" miniaturizable="YES" resizable="YES"/>
<windowPositionMask key="initialPositionMask" leftStrut="YES" rightStrut="YES" topStrut="YES" bottomStrut="YES"/>
Expand Down Expand Up @@ -646,7 +658,7 @@
<rect key="frame" x="1" y="1" width="463" height="268"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<textView importsGraphics="NO" horizontallyResizable="YES" findStyle="panel" continuousSpellChecking="YES" allowsUndo="YES" usesRuler="YES" usesFontPanel="YES" verticallyResizable="YES" allowsNonContiguousLayout="YES" quoteSubstitution="YES" dashSubstitution="YES" smartInsertDelete="YES" id="Yey-g8-OWP">
<textView editable="NO" importsGraphics="NO" horizontallyResizable="YES" findStyle="panel" continuousSpellChecking="YES" allowsUndo="YES" usesRuler="YES" usesFontPanel="YES" verticallyResizable="YES" allowsNonContiguousLayout="YES" quoteSubstitution="YES" dashSubstitution="YES" smartInsertDelete="YES" id="Yey-g8-OWP">
<rect key="frame" x="0.0" y="0.0" width="223" height="268"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
Expand Down
1 change: 1 addition & 0 deletions bilibili/BilibiliSocketClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- (bool)ConnectToTheFuckingFlashSocketServer: (int)roomid;
- (void)setDelegate:(id)del;
- (void)Disconnect;

@end

Expand Down
14 changes: 12 additions & 2 deletions bilibili/BilibiliSocketClient.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

@implementation LiveSocket{
id delegate;
bool disconnected;
NSTimer *hbTimer;
}
- (void)setDelegate:(id)del{
delegate = del;
Expand All @@ -26,7 +28,7 @@ - (bool)ConnectToTheFuckingFlashSocketServer: (int)roomid{
return false;
}

[NSTimer scheduledTimerWithTimeInterval:30.0
hbTimer = [NSTimer scheduledTimerWithTimeInterval:30.0
target:self
selector:@selector(startHB)
userInfo:nil
Expand All @@ -40,10 +42,17 @@ - (bool)ConnectToTheFuckingFlashSocketServer: (int)roomid{
return true;
}

- (void)Disconnect{
disconnected = true;
[hbTimer invalidate];
hbTimer = nil;
c.disconnect😈();
}

- (void) recvMsg{
dispatch_queue_t q = dispatch_queue_create("com.typcn.bilisocket", NULL);
dispatch_async(q, ^(void){
while (true) {
while (!disconnected) {
std::string str = c.receive(2048);
if(str.length() > 4){
NSString *recv = [NSString stringWithUTF8String:str.c_str()];
Expand All @@ -57,6 +66,7 @@ - (void) recvMsg{

}
}
NSLog(@"Loop end");
});
}

Expand Down
4 changes: 2 additions & 2 deletions bilibili/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.7b</string>
<string>1.8</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
Expand All @@ -32,7 +32,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>16</string>
<string>17</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.video</string>
<key>LSMinimumSystemVersion</key>
Expand Down
19 changes: 16 additions & 3 deletions bilibili/LiveChat.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

BOOL hasMsg;

@interface LiveChat ()
@interface LiveChat (){
LiveSocket *socket;
}
@property (unsafe_unretained) IBOutlet NSTextView *textView;

@end
Expand All @@ -23,9 +25,11 @@ @implementation LiveChat

- (void)viewDidLoad {
[super viewDidLoad];
LiveSocket *socket = [[LiveSocket alloc] init];
socket = [[LiveSocket alloc] init];
[socket setDelegate:self];
[socket ConnectToTheFuckingFlashSocketServer:[vCID intValue]];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(windowWillClose:) name:NSWindowWillCloseNotification object:self.view.window];
}

- (void)onNewMessage:(NSDictionary *)data{
Expand All @@ -44,12 +48,21 @@ - (void)onNewError:(NSString *)str{

- (void)AppendToTextView:(NSString *)text{
dispatch_async(dispatch_get_main_queue(), ^{
NSAttributedString* attr = [[NSAttributedString alloc] initWithString:text];
NSFont *font = [NSFont fontWithName:@"Helvetica" size:15.0];
NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:font
forKey:NSFontAttributeName];
NSAttributedString* attr = [[NSAttributedString alloc] initWithString:text attributes:attrsDictionary];

[[self.textView textStorage] appendAttributedString:attr];
[self.textView scrollRangeToVisible:NSMakeRange([[self.textView string] length], 0)];
});
}

- (void)windowWillClose:(NSNotification *)notification
{
NSLog(@"Disconnecting");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[socket Disconnect];
}

@end
1 change: 1 addition & 0 deletions bilibili/PlayerView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@property (weak) IBOutlet NSTextField *subtip;
@property (weak) IBOutlet NSImageView *loadingImage;
@property (weak) IBOutlet NSButton *PostCommentButton;
@property (weak) IBOutlet NSButton *showLiveChat;

- (NSDictionary *) getVideoInfo:(NSString *)url;
- (NSString *) getComments:(NSNumber *)width :(NSNumber *)height;
Expand Down
4 changes: 4 additions & 0 deletions bilibili/PlayerView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ - (void)PlayVideo:(NSString*) commentFile :(NSString*)res{
if(![vUrl containsString:@"live_"]){
check_error(mpv_set_option_string(mpv, "sub-ass", "yes"));
check_error(mpv_set_option_string(mpv, "sub-file", [commentFile cStringUsingEncoding:NSUTF8StringEncoding]));
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[self.showLiveChat performClick:nil];
});
}
// request important errors
check_error(mpv_request_log_messages(mpv, "warn"));
Expand Down
7 changes: 7 additions & 0 deletions bilibili/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,10 @@ string tcp_client::receive(int size=512)
string reply(buffer);
return reply;
}

void tcp_client::disconnect😈()
{
shutdown(sock, SHUT_RDWR);
sock = NULL;
rec_port = NULL;
}
1 change: 1 addition & 0 deletions bilibili/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class tcp_client{
bool conn(std::string, int);
bool send_data(const void *data,int size);
std::string receive(int);
void disconnect😈();
};

#endif /* defined(__bilibili__Socket__) */

0 comments on commit 9d914a6

Please sign in to comment.