xxGameInstance.h
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "IWebSocket.h"
#include "WebSocketGameInstance.generated.h"
/** * */
UCLASS()
class WEBSOCKETDEMO_API UWebSocketGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
virtual void Init() override;
virtual void Shutdown() override;
TSharedPtr<IWebSocket> WebSocket;
};
xxGameInstance.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "WebSocketGameInstance.h"
#include "WebSocketsModule.h"
void UWebSocketGameInstance::Init() {
Super::Init();
if (!FModuleManager::Get().IsModuleLoaded("WebSockets")) {
FModuleManager::Get().LoadModule("WebSockets");
}
WebSocket = FWebSocketsModule::Get().CreateWebSocket("ws://127.0.0.1:7890");
WebSocket->OnConnected().AddLambda([]() {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Green, "Successfully Connected");
});
WebSocket->OnMessage().AddLambda([](const FString& MessageString) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "Received Message: " + MessageString);
});
WebSocket->OnMessageSent().AddLambda([](const FString& MessageString) {
GEngine->AddOnScreenDebugMessage(-1, 3.0f, FColor::Cyan, "Sent Message: " + MessageString);
});
WebSocket->Connect();
}
void UWebSocketGameInstance::Shutdown() {
if (WebSocket->IsConnected()) {
WebSocket->Close();
}
Super::Shutdown();
}
void AWebsocketDemoCharacter::NotifyServer() {
UWebSocketGameInstance* GameInstance = Cast<UWebSocketGameInstance>(GetGameInstance());
if (GameInstance) {
if (GameInstance->WebSocket->IsConnected()) {
GameInstance->WebSocket->Send("FROM UnrealEngine 4");
}
}
}
void AWebsocketDemoCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up gameplay key bindings
check(PlayerInputComponent);
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &ACharacter::Jump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &ACharacter::StopJumping);
PlayerInputComponent->BindAxis("MoveForward", this, &AWebsocketDemoCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AWebsocketDemoCharacter::MoveRight);
// We have 2 versions of the rotation bindings to handle different kinds of devices differently
// "turn" handles devices that provide an absolute delta, such as a mouse.
// "turnrate" is for devices that we choose to treat as a rate of change, such as an analog joystick
PlayerInputComponent->BindAxis("Turn", this, &APawn::AddControllerYawInput);
PlayerInputComponent->BindAxis("TurnRate", this, &AWebsocketDemoCharacter::TurnAtRate);
PlayerInputComponent->BindAxis("LookUp", this, &APawn::AddControllerPitchInput);
PlayerInputComponent->BindAxis("LookUpRate", this, &AWebsocketDemoCharacter::LookUpAtRate);
// handle touch devices
PlayerInputComponent->BindTouch(IE_Pressed, this, &AWebsocketDemoCharacter::TouchStarted);
PlayerInputComponent->BindTouch(IE_Released, this, &AWebsocketDemoCharacter::TouchStopped);
// VR headset functionality
PlayerInputComponent->BindAction("ResetVR", IE_Pressed, this, &AWebsocketDemoCharacter::OnResetVR);
PlayerInputComponent->BindAction("NotifyServer", IE_Pressed, this, &AWebsocketDemoCharacter::NotifyServer);
}
import websockets
import asyncio
import json
global data
PORT = 7890
print("Running on PORT 7890")
async def echo(websocket,path):
global data
print("A client just connected")
async for message in websocket:
print(message)
await websocket.send("From Python")
start_server = websockets.serve(echo,"127.0.0.1",PORT)
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()