<commit_before>use super::{Result, Error};

use std::sync::mpsc;

use websocket::ws::sender::Sender as SenderTrait;
use websocket::client::{Client, Sender, Receiver};
use websocket::stream::WebSocketStream;
use websocket::message::{Message as WsMessage, Type as MessageType};

use serde_json;
use serde_json::builder::ObjectBuilder;

use super::model::*;

/// The websocket protocol version expected.
const VERSION: u64 = 3;

/// Websocket connection to the Discord servers.
pub struct Connection {
	keepalive_channel: mpsc::Sender<Status>,
	receiver: Receiver<WebSocketStream>,
	ready_event: Option<Event>,
	/// Known state composed from received events
	pub state: State,
}

impl Connection {
	pub fn new(url: &str, token: &str) -> Result<Connection> {
		// establish the websocket connection
		let url = match ::websocket::client::request::Url::parse(url) {
			Ok(url) => url,
			Err(_) => return Err(Error::Other("Invalid URL in Connection::new()"))
		};
		let response = try!(try!(Client::connect(url)).send());
		try!(response.validate());
		let (mut sender, mut receiver) = response.begin().split();
		
		// send the handshake
		let map = ObjectBuilder::new()
			.insert("op", 2)
			.insert_object("d", |object| object
				.insert("token", token)
				.insert_object("properties", |object| object
					.insert("$os", ::std::env::consts::OS)
					.insert("$browser", "Howl library for Rust")
					.insert("$device", "howl")
					.insert("$referring_domain", "")
					.insert("$referrer", "")
				)
				.insert("v", 3)
			)
			.unwrap();
		try!(sender.send_message(&WsMessage::text(try!(serde_json::to_string(&map)))));

		// read the Ready event
		let ready = try!(recv_message(&mut receiver));
		let state = try!(State::new(ready.clone()));
		let heartbeat_interval = state.heartbeat_interval;

		// spawn the keepalive thread
		let (tx, rx) = mpsc::channel();
		::std::thread::spawn(move || keepalive(heartbeat_interval, sender, rx));

		// return the connection
		Ok(Connection {
			keepalive_channel: tx,
			receiver: receiver,
			ready_event: Some(ready),
			state: state,
		})
	}

	pub fn set_game_id(&mut self, game_id: Option<u64>) {
		let _ = self.keepalive_channel.send(Status::SetGameId(game_id));
	}

	pub fn recv_event(&mut self) -> Result<Event> {<commit_msg>Adjust user-agent information<commit_after>use super::{Result, Error};

use std::sync::mpsc;

use websocket::ws::sender::Sender as SenderTrait;
use websocket::client::{Client, Sender, Receiver};
use websocket::stream::WebSocketStream;
use websocket::message::{Message as WsMessage, Type as MessageType};

use serde_json;
use serde_json::builder::ObjectBuilder;

use super::model::*;

/// The websocket protocol version expected.
const VERSION: u64 = 3;

/// Websocket connection to the Discord servers.
pub struct Connection {
	keepalive_channel: mpsc::Sender<Status>,
	receiver: Receiver<WebSocketStream>,
	ready_event: Option<Event>,
	/// Known state composed from received events
	pub state: State,
}

impl Connection {
	pub fn new(url: &str, token: &str) -> Result<Connection> {
		// establish the websocket connection
		let url = match ::websocket::client::request::Url::parse(url) {
			Ok(url) => url,
			Err(_) => return Err(Error::Other("Invalid URL in Connection::new()"))
		};
		let response = try!(try!(Client::connect(url)).send());
		try!(response.validate());
		let (mut sender, mut receiver) = response.begin().split();
		
		// send the handshake
		let map = ObjectBuilder::new()
			.insert("op", 2)
			.insert_object("d", |object| object
				.insert("token", token)
				.insert_object("properties", |object| object
					.insert("$os", ::std::env::consts::OS)
					.insert("$browser", "Discord library for Rust")
					.insert("$device", "discord-rs")
					.insert("$referring_domain", "")
					.insert("$referrer", "")
				)
				.insert("v", 3)
			)
			.unwrap();
		try!(sender.send_message(&WsMessage::text(try!(serde_json::to_string(&map)))));

		// read the Ready event
		let ready = try!(recv_message(&mut receiver));
		let state = try!(State::new(ready.clone()));
		let heartbeat_interval = state.heartbeat_interval;

		// spawn the keepalive thread
		let (tx, rx) = mpsc::channel();
		::std::thread::spawn(move || keepalive(heartbeat_interval, sender, rx));

		// return the connection
		Ok(Connection {
			keepalive_channel: tx,
			receiver: receiver,
			ready_event: Some(ready),
			state: state,
		})
	}

	pub fn set_game_id(&mut self, game_id: Option<u64>) {
		let _ = self.keepalive_channel.send(Status::SetGameId(game_id));
	}

	pub fn recv_event(&mut self) -> Result<Event> {
{'fraction_non_alphanumeric': 0.16764898493778652, 'fraction_numerical': 0.0030561012879283997, 'mean_word_length': 4.749058971141782, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5898841', 'n_tokens_mistral': 1659, 'n_tokens_neox': 1490, 'n_words': 358}